2011-02-11 47 views
0

我一直在努力在垂直LinearLayout佈局兩個ListViews。我終於在這裏看到了關於在自己的LinearLayout中包裝每個LinearLayout的答案,並且將這些LinearLayout中的每一個添加到原始LinearLayout,權重爲1.所以這就是我的嘗試......但我似乎無法得到此工作。問題佈置兩個ListViews

我做這一切的代碼:

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); 
    } 
} 

這是我最新的嘗試,但我發誓,我已經試過各種組合/設置爲的LayoutParams'高度(FILL_PARENT,WRAP_CONTENT,0)作爲以及各種重量。

結果永遠不會很好;如果我的第二個列表很長,它不可避免地佔據了原始垂直佈局的絕大部分(當然大大超過50%)。

我不會放棄!也許有人可以幫我一把。

感謝, 肯

+0

不知道這是否與此相關,但我認爲與addView方法有所不同。你可以嘗試調用:`addView(_layout1,new LayoutParams(LayoutParams.FILL_PARENT,0,1));`而不是事先設置LayoutParams。 `_layout2`一樣。 – 2011-02-11 05:16:50

+0

@塞巴斯蒂安 - 那不會有什麼區別。 `addView(aView)`和`addView(aView,aView.getLayoutParams())`完全一樣。 – 2011-02-11 07:01:17

+0

我不太清楚爲什麼你需要_layout1和_layout2。爲什麼不把_list1和_list2的高度設置爲0,它們的權重爲1,並將它們直接添加到雙列表中? – 2011-02-11 07:06:45

回答

1

要更換正確的佈局PARAMS(你setLayoutParams設置的)使用不正確的佈局PARAMS(你在addView傳遞一個。)只需撥打addView(_list1)來代替,而不指定佈局參數。 編輯:對不起,我看錯了代碼,請忽略此:)

2

我想請您查看邏輯移動到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代碼可能是:

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)); 

此外,如果您堅持要爲UI編寫Java代碼,我會嘗試將LayoutParams設置爲addView(_layout1) - >addView(_layout1, new LayoutParams(LayoutParams.FILL_PARENT, 0, 1))

順便說一句:你在哪裏設置這個佈局的方向?默認爲橫向

0

因此,這裏是溶液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)); 
     } 
    } 
} 

哪個生產:

Screenshot

編輯:

在填充DATA2與1000個條目:

final String[] data2 = new String[1000]; 
for(int i = 0; i < 1000; i++) { 
    data2[i] = "test" + i; 
} 

我仍然得到同樣的佈局:

Screenshot 50/50 * 1000 entries

編輯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> 

足夠也許好? ;-)