Problem laying out two ListViews
I've been struggling to lay out two ListViews in a vertical LinearLayout. I finally saw the answer on here about wrapping each in their own LinearLayout, and adding each one of these LinearLayouts to the original LinearLayout with weights of 1. So that's what I tried... but I just cannot seem to get this to work.
I'm doing this all in code:
public class MyDualList extends LinearLayout
{
private LinearLayout _layout1;
private LinearLayout _layout2;
private ListView _list1;
private ListView _list2;
public MyDualList(Context context, ListView list1, ListView list2)
{
super(context);
_list1 = list1;
_list2 = list2;
_layout1 = new LinearLayout(context);
_layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1));
_layout1.addView(_list1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
_layout2 = new LinearLayout(context);
_layout2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1));
_layout2.addView(_list2, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
addView(_layout1);
addView(_layout2);
}
}
This is my latest attempt, but I swear I've tried every combination/setting for the LayoutParams' height (FILL_PARENT, WRAP_CONTENT, 0) as well as various weights.
The result is never very good; if my second list is very 开发者_如何学编程long, it inevitably takes up the vast majority of the original vertical layout (certainly much more than 50%).
I'm not going to give up! Maybe someone can help me out a bit.
Thanks, Ken
I like to ask you to move view logic to XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
Java code could be:
final String[] data1 = new String[] { "1a", "1b", "1c", "1d", "1e", "1f", "1g", "1h", "1i" };
final String[] data2 = new String[] { "2a", "2b", "2c", "2d", "2e", "2f", "2g", "2h", "2i" };
final ListView l1 = (ListView) findViewById(R.id.list1);
final ListView l2 = (ListView) findViewById(R.id.list2);
l1.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data1));
l2.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data2));
Additionally, if you insist on Java coding for the UI, I would try to set the LayoutParams
in your calls to addView(_layout1)
-> addView(_layout1, new LayoutParams(LayoutParams.FILL_PARENT, 0, 1))
.
BTW: Where do you set the orientation of this layout? Default is horizontal.
You are replacing the correct layout params (the ones you set with setLayoutParams) with incorrect layout params (the one you pass in addView.) Just call addView(_list1) instead, without specifying layout params. Edit: Sorry, I misread the code, ignore this :)
So here is solution 2:
public class Q4965745 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
setContentView(new Q4965745View(this));
final String[] data1 = new String[]{"1a", "1b", "1c", "1d", "1e", "1f", "1g", "1h", "1i"};
final String[] data2 = new String[]{"2a", "2b", "2c", "2d", "2e", "2f", "2g", "2h", "2i"};
final ListView l1 = (ListView) findViewById(11);
final ListView l2 = (ListView) findViewById(22);
l1.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data1));
l2.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data2));
}
class Q4965745View extends LinearLayout {
public Q4965745View(Context context) {
super(context);
Q4965745View.this.setOrientation(LinearLayout.VERTICAL);
ListView lv1 = new ListView(context);
lv1.setId(11);
ListView lv2 = new ListView(context);
lv2.setId(22);
Q4965745View.this.addView(lv1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
Q4965745View.this.addView(lv2, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
}
}
}
Which produces:
EDIT:
When filling data2 with 1000 entries:
final String[] data2 = new String[1000];
for(int i = 0; i < 1000; i++) {
data2[i] = "test" + i;
}
I still get the same layout:
EDIT 2:
I was able to get a "better behavior" at least by using TableLayout
:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
Maybe Romain has an idea on how to make it really 50/50 independent of the amount of items in the ListView
s..
EDIT 3:
Ok, final solution (using inner LinearLayout):
<?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:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Maybe good enough? ;-)
精彩评论