2011-09-27 28 views
1

我有tablelayout的Tablerow的Linearlayout部分。下面是樣本描述如何在擴展LinearLayout時添加行邊距?

<!-- Master Layout--> 
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" android:layout_width="match_parent" 
    android:stretchColumns="0" android:baselineAligned="false"> 
<TableRow> 
<Button></Button> 
</Tablerow> 

<TableRow> 
<LinearLayout android:id="@+id/listinfo"></LinearLayout> 
</Tablerow> 

然後,我又寫了LinearLayout中創建的LinearLayout,我膨脹,並加入到 的LinearLayout主要佈局的行。

<!-- listrow layout--> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android" 

    <ImageView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_gravity="left" 
     android:src="@drawable/icon" /> 

     <TextView android:layout_weight="1" android:id="@+id/textView1" /> 

     <TextView android:layout_weight="1" android:id="@+id/textView1" /> 

</LinearLayout> 

一切工作正常,但我沒有得到行劃界。怎麼做?

Java代碼

LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listinfo); 

for (int i =0; i < myarray.size(); i++) { 

    final Employee eobj = myarray.get(i);  
    LayoutInflater vi =   (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null); 
    lrow.setBackgroundColor(android.graphics.Color.WHITE); 
    lrow.setPadding(2, 2, 2, 2); 
    ((TextView)lrow.getChildAt(1)).setText(eobj.getName());  

    //some more settings 
    placeHolderLinearLayout.addView(lrow); 

} 

的視圖不顯示後續的LinearLayout之間的任何demarcator。我怎樣才能做到這一點?

|---------------------| 
    | lrow1    | 
    |______demarcator_____| 
    | lrow2    | => The demaractor is missing in my view 
    |_____________________| 

回答

4
LinearLayout lrow = (LinearLayout)vi.inflate(R.layout.listrow, null); 

試圖通過placeHolderLinearLayout代替null

如果您將true定義爲第三個參數,則調整後的佈局將自動添加到給定ViewGroupfalse會避免這種情況。

+0

我的問題不是這樣。所有視圖都得到正確添加,但我無法看到兩個後續行之間的邊距。兩排之間沒有劃分,它們也不重疊,看起來完全沒問題,但我想要一些反面意見。 –

+0

1.您的示例中沒有單個邊距定義。 2.那麼你在談論哪個邊緣?你期望哪些視圖有maregins(我認爲你的意思是在你的第二個XML佈局代碼中的根'LinearLayout') – Knickedi

+0

邊距我的意思是行分界符。是的,我在其中添加lrows的根LinearLayout不顯示兩個後續行之間的任何分界符。我會在我的問題中以圖片形式解釋它。 –