【android】巧用android:divider属性设置LinearLayout中元素之间的间隔_第1张图片


如上图,要想实现3个button线性排列并且使它们的大小相同、间隔相等、而且整体填充满整个linearlayout,我们一般的做法是在每两个button之间放一个固定宽度的view,然后设置button的宽度为0、layout_weight为1。这样虽能实现功能,可是总感觉不方便,特别是button多的时候。


今天介绍另一种简单、优雅的方法,就是利用android:divider属性。


1、首先新建一个固有的width/height的Drawable

spacer_medium.xml

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns: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. <solidandroid:color="@android:color/transparent"/>
  8. </shape>

2.然后设置LinearLayout的android:divider="@drawable/spacer_medium",并设置android:showDividers="middle",这样就完美解决了linearLayout的元素之间的间隔问题。

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


参考:http://blog.csdn.net/startupmount/article/details/41745715




更多相关文章

  1. 关于android:tint属性的使用
  2. Android edittext 属性inputtype详解
  3. Android 四种获取屏幕宽度的方法总结
  4. ImageView属性小结

随机推荐

  1. 在Android(安卓)Studio中自定义字体库报
  2. Android弹幕实现:基于B站弹幕开源系统(2)
  3. android 中文API:android
  4. Android之使用Android-query框架开发实战
  5. 2016年3月1日Android实习笔记
  6. android抽屉效果
  7. Android中ImageView和VideoView铺满父元
  8. Android(安卓)分析内存的使用情况
  9. android调用unity
  10. Android Step by Step (1)——第一个Andr