[Android] uses the android: divider attribute to set the interval between elements in LinearLayout,
For example, to linearly arrange three buttons and make them of the same size, equal intervals, and full filling of the entire linearlayout, we usually place a fixed-width view between each two buttons, and set the width of the button to 0 and layout_weight to 1. In this way, functions can be implemented, but it is inconvenient, especially when there are many buttons.
Another simple and elegant method is to use the android: divider attribute.
1. First create a Drawable with inherent width/height:
Spacer_medium.xml
[Java]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: shape = "rectangle">
- <Size
- Android: width = "@ dimen/spacing_medium"
- Android: height = "@ dimen/spacing_medium"/>
- <Solid android: color = "@ android: color/transparent"/>
- </Shape>
2. Set android: divider = "@ drawable/spacer_medium" of LinearLayout, and set android: showDividers = "middle". This perfectly solves the issue of the interval between linearLayout elements.
<LinearLayout android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:divider="@drawable/spacer_medium" android:background="#77000000" android:showDividers="middle"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="button"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="button"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="button"/> </LinearLayout>
Reference: http://blog.csdn.net/startupmount/article/details/41745715