开发者

How to add row margin while inflating a LinearLayout?

开发者 https://www.devze.com 2023-04-07 20:43 出处:网络
I have a Linearlayout part of a Tablerow of tablelayout. Below is sample description <!-- Master Layout-->

I have a Linearlayout part of a Tablerow of tablelayout. Below is sample description

<!-- 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>

Then I wrote another linearlayout to create rows of linearlayout which I inflate and add to linearlayout of Main Layout.

开发者_运维知识库<!-- 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>

Everything is working perfectly fine but I am not getting row demarcators. How to do that?

Java code

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

 }

The view doesn't show any demarcator between subsequent linearlayout. How can I achieve that?

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


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

Try to pass placeHolderLinearLayout instead null.

If you define true as third parameter the infalted layout will be added automatically to the given ViewGroup. false will avoid that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号