I am programatically adding custom views to a vertical LinearLayout, and I would like there to be some space between the views. I have tried adding: setPadding(0, 1, 0, 1)
The sample below just does what you need programatically. I have used a fixed size of (140,398).
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
layoutParams.setMargins(24, 0, 24, 0);
layout.addView(button,layoutParams);
Try to add Space widget after adding view like this:
layout.addView(view)
val space = Space(context)
space.minimumHeight = spaceInterval
layout.addView(space)
Using padding in the layout of Child View.
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/backage_text"
android:textColor="#999999"
>
</TextView>
backage_text.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners android:radius="2dp"/>
<stroke
android:width="1dp"
android:color="#999999"/>
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape>
Since API Level 14 you can just add a (transparent) divider drawable:
android:divider="@drawable/divider"
android:showDividers="middle"
and it will handle the rest for you!
If you use ActionBarSherlock, you can use com.actionbarsherlock.internal.widget.IcsLinearLayout :
<com.actionbarsherlock.internal.widget.IcsLinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/list_view_divider"
android:dividerPadding="2dp"
android:showDividers="middle" >
...
</com.actionbarsherlock.internal.widget.IcsLinearLayout>
You just need to wrap items with linear layouts which have layout_weight. To have items horizontally separated, use this
<LinearLayout
...
...
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
// your item
</LinearLayout>
</LinearLayout>