[Android] uses the android: divider attribute to set the interval between elements in LinearLayout,

Source: Internet
Author: User

[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
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Shape xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: shape = "rectangle">
  4. <Size
  5. Android: width = "@ dimen/spacing_medium"
  6. Android: height = "@ dimen/spacing_medium"/>
  7. <Solid android: color = "@ android: color/transparent"/>
  8. </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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.