因此,這裏是溶液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));
}
}
}
哪個生產:
編輯:
在填充DATA2與1000個條目:
final String[] data2 = new String[1000];
for(int i = 0; i < 1000; i++) {
data2[i] = "test" + i;
}
我仍然得到同樣的佈局:
編輯2:
我能得到一個 「好行爲」 至少使用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>
也許羅曼有一個想法,如何使它真正50/50 independe在ListView
s個項目的量的NT ..
EDIT 3:
確定,最終的解決方案(使用內的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>
足夠也許好? ;-)
不知道這是否與此相關,但我認爲與addView方法有所不同。你可以嘗試調用:`addView(_layout1,new LayoutParams(LayoutParams.FILL_PARENT,0,1));`而不是事先設置LayoutParams。 `_layout2`一樣。 – 2011-02-11 05:16:50
@塞巴斯蒂安 - 那不會有什麼區別。 `addView(aView)`和`addView(aView,aView.getLayoutParams())`完全一樣。 – 2011-02-11 07:01:17
我不太清楚爲什麼你需要_layout1和_layout2。爲什麼不把_list1和_list2的高度設置爲0,它們的權重爲1,並將它們直接添加到雙列表中? – 2011-02-11 07:06:45