Android 线性布局中的两个列表视图
请帮助我在线性布局中垂直放置两个列表视图的布局。问题是,若列表1的数据量大于列表2,则该列表不可见且不可滚动,则使用scollview并不是解决方案Android 线性布局中的两个列表视图,android,android-listview,Android,Android Listview,请帮助我在线性布局中垂直放置两个列表视图的布局。问题是,若列表1的数据量大于列表2,则该列表不可见且不可滚动,则使用scollview并不是解决方案 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_calendar_top"
android:orientation="horizontal" >
<ImageView
android:id="@+id/prevMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="@drawable/button_events_previous" >
</ImageView>
<TextView
android:id="@+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center_vertical|center_horizontal"
android:text="Janauary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#d458b1"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="@+id/nextMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="@drawable/button_events_next" />
</LinearLayout>
<TextView
android:id="@+id/direction_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="PRESS RELEASES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="@+id/eventsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/direction_label"
android:background="#ffffff" >
</ListView>
<TextView
android:id="@+id/direction_label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="SPEECHES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="@+id/eventsList1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/direction_label1"
android:background="#ffffff" >
</ListView>
<TextView
android:id="@+id/empty_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
对于第二个ListView,它如下所示:
if ((year == event_year) && (event_month == month))
{
arrayofWebDataPress.add(cn);
listAdapter_press = new SelectArralAdapter_Press(getActivity(),arrayofWebDataPress);
List_events_press.setAdapter(listAdapter_press);
i++;
}
两个ListView的阵列适配器:
class SelectArralAdapter_Press extends ArrayAdapter<PressDB> {
private LayoutInflater inflater;
public SelectArralAdapter_Press(Context context,
ArrayList<PressDB> arrayofWebDataPress) {
super(context, R.layout.speech_list_item, R.id.event_title,
arrayofWebDataPress);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder_Press holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.speech_list_item, null);
holder = new ViewHolder_Press(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder_Press) convertView.getTag();
}
holder.populateFrom(arrayofWebDataPress.get(position));
return (convertView);
}
}
class ViewHolder_Press{
public TextView event_name = null;
public TextView event_date = null;
public ViewHolder_Press(View row) {
event_name = (TextView) row.findViewById(R.id.event_title);
event_date = (TextView) row.findViewById(R.id.event_date_time);
}
void populateFrom(PressDB eventsMainDB) {
event_name.setText(eventsMainDB.press_name);
event_date.setText(eventsMainDB.press_date + " ");
}
}
class SelectArralAdapter\u按扩展ArrayAdapter{
私人充气机;
public SelectArralAdapter_Press(上下文),
ArrayList arrayofWebDataPress){
超级(上下文、R.layout.speech\u列表项目、R.id.event\u标题、,
arrayofWebDataPress);
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座和压具座;
if(convertView==null){
convertView=充气机。充气(R.layout.speech\u列表项,空);
保持架=新视图保持架(转换视图);
convertView.setTag(支架);
}否则{
holder=(ViewHolder_Press)convertView.getTag();
}
holder.populateFrom(arrayofWebDataPress.get(位置));
返回(转换视图);
}
}
类视窗夹持器{
公共文本视图事件\ u name=null;
public TextView event_date=null;
公共视图持有人\按(视图行){
事件名称=(TextView)行.findViewById(R.id.event\u title);
event\u date=(TextView)row.findViewById(R.id.event\u date\u time);
}
无效的populateFrom(按DB EventsMindB){
event_name.setText(eventsMainDB.press_name);
event_date.setText(eventsMainDB.press_date+“”);
}
}
对于第二个适配器:
class SelectArralAdapter extends ArrayAdapter<SpeechDB> {
private LayoutInflater inflater;
public SelectArralAdapter(Context context,
ArrayList<SpeechDB> arrayofWebData) {
super(context, R.layout.speech_list_item, R.id.event_title,
arrayofWebData);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.speech_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayofWebData.get(position));
return (convertView);
}
}
class ViewHolder {
public TextView event_name = null;
public TextView event_date = null;
public ViewHolder(View row) {
event_name = (TextView) row.findViewById(R.id.event_title);
event_date = (TextView) row.findViewById(R.id.event_date_time);
}
void populateFrom(SpeechDB eventsMainDB) {
event_name.setText(eventsMainDB.speech_name);
event_date.setText(eventsMainDB.speech_date + " ");
}
}
class SelectArralAdapter扩展了ArrayAdapter{
私人充气机;
public SelectArralAdapter(上下文,
ArrayList arrayofWebData){
超级(上下文、R.layout.speech\u列表项目、R.id.event\u标题、,
arrayofWebData);
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=充气机。充气(R.layout.speech\u列表项,空);
支架=新的视图支架(convertView);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.populateFrom(arrayofWebData.get(位置));
返回(转换视图);
}
}
类视图持有者{
公共文本视图事件\ u name=null;
public TextView event_date=null;
公共视图持有者(视图行){
事件名称=(TextView)行.findViewById(R.id.event\u title);
event\u date=(TextView)row.findViewById(R.id.event\u date\u time);
}
无效populateFrom(SpeechDB事件索引){
event_name.setText(eventsMainDB.speech_name);
event_date.setText(eventsMainDB.speech_date+);
}
}
你可以拿这样的东西
<LinearLayout>
<ScrollView>
<LinearLayout>
{Your All code of controls}
</LinearLayout>
</ScrollView>
</LinearLayout>
{您的所有控件代码}
或者uo可以将
作为父标记。你可以这样做。据我所知…我曾使用过:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
// first linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="@+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// second linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="@+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// third linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="@+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// and finally i have added a button in the parent linearlayout
<Button
/>
</LinearLayout>
</ScrollView>
//第一个linearlayout包含一个textview和一个listview
//第三个线性布局,包含一个文本视图和一个列表视图
试试这个
如果您想使用xml设计,那么您必须通过使用权重(这是最可取的)或在dp中给出固定大小来给出固定大小
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backrepeat"
android:weightSum="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_calendar_top"
android:orientation="horizontal" >
<ImageView
android:id="@+id/prevMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="@drawable/button_events_previous" >
</ImageView>
<TextView
android:id="@+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center_vertical|center_horizontal"
android:text="Janauary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#d458b1"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="@+id/nextMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="@drawable/button_events_next" />
</LinearLayout>
<TextView
android:id="@+id/direction_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="PRESS RELEASES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="@+id/eventsList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_below="@+id/direction_label"
android:background="#ffffff" >
</ListView>
<TextView
android:id="@+id/direction_label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="SPEECHES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="@+id/eventsList1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_below="@+id/direction_label1"
android:background="#ffffff" >
</ListView>
<TextView
android:id="@+id/empty_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我在设计时得到这个屏幕
按如下方式进行尝试:
以下是解决同一布局文件中多个ListView的方法。通过为线性布局
中的每个布局提供相等的权重,可以将布局文件设置为以下布局
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="@+id/list1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="@+id/list2"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
Java代码
public class MainActivity extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "e", "f"};
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1 = (ListView) findViewById (R.id.list1);
lv2 = (ListView) findViewById (R.id.list2);
lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));
}
}
公共类MainActivity扩展活动{
私有ListView lv1=null;
私有ListView lv2=null;
私有字符串s1[]={“a”、“b”、“c”、“d”、“e”、“f”};
私有字符串s2[]={“r”、“s”、“t”、“u”、“v”、“w”、“x”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.list1);
lv2=(ListView)findViewById(R.id.list2);
lv1.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,s1));
lv2.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,s2));
}
}
有关更多详细信息,请参阅提供了相同解决方案的
我希望它能帮助你
谢谢 我通过使用节标题解决了这个问题,并将两个listview合并到一个listview中,并通过节标题对它们进行分类。这里的类似问题您推荐的解决方案我尝试了相对布局,它不起作用。您可以尝试将layout\u weight=1
属性放在ListView
上,两者的比例相同。请记住将layout\u height
更改为0
而不是wrap\u content
。您能详细说明您的问题吗?没有给出固定的大小或重量??您的意思是,当您运行此代码时,您将面临此问题。不,它运行成功,但是,listview和java代码的listview都是空的。您可能没有在代码中设置listview上的另一个布局。请检查我也得到相同的屏幕设计显示我将两个相对的布局组合成一些东西这使屏幕分为两部分,上面一部分为1 listview,下面一部分为另一部分1,每个部分都有自己的listview滚动条,我可以有类似的整体吗
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="@+id/list1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="@+id/list2"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
public class MainActivity extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "e", "f"};
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1 = (ListView) findViewById (R.id.list1);
lv2 = (ListView) findViewById (R.id.list2);
lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));
}
}