android:divider="@drawable/shape"<!--分割线图片--> android:showDividers="middle|beginning|end"<!--分割线位置--> 分割线如果是图片那就直接使用图片就行,如果要使用颜色就必须使用shape来显示,直接使用颜色或Color是没有用的
使用shape的时候要注意设置size属性不设置宽高分割线就不会显示出来,如果使用line那填充颜色只能使用stroke来显示颜色
例子:
<LinearLayout            android:id="@+id/buttons_container"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:divider="@drawable/spacer_medium"            android:orientation="horizontal"            android:showDividers="middle">              <Button                    android:id="@+id/btn_first"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                                       android:text="button_1" />                          <Button                    android:id="@+id/btn_second"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                                          android:text="button_2" />                          <Button                    android:id="@+id/btn_third"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                                          android:text="button_3" />              </LinearLayout>

spacer_medium定义如下:

<?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="@color/divider_color" />  </shape>

如果我们想要很简单的给linearLayout设置空隙,就不用填充颜色了。比如这样:
<?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>

而且在你动态改变元素的时候也不会出现bug。

这里指的bug是:

我们用传统的方式设置空隙,在显示三个按钮的时候没有问题,但在动态改变成两个按钮的时候就会出现问题。

1.三个按钮的时候

2.两个按钮的时候

用了上述的方法后,每次的布局间距都会是一致的了。 参考自: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2268.html http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2266.html

更多相关文章

  1. Android按钮按下时和弹起时的颜色设置
  2. tab2
  3. 自定义ImageView重写onTouchEvent
  4. Web初试
  5. Material Design CheckBox 单独更改颜色
  6. 修改状态栏颜色和状态栏字体颜色2
  7. Android(安卓)小功能之 TextView 只设置某些文字可点击
  8. Android(安卓)开发入门-活动的基本用法
  9. Android(安卓)无入侵解决按钮重复点击---DoubleClick2.0的使用

随机推荐

  1. Java 线程面试题 Top 50
  2. 推荐一款神仙颜值的 ZooKeeper 客户端工
  3. Hibernate 和 UUID 标示符
  4. 内推工作福利
  5. 数据库界的 Swagger,一键自动生成 Java 实
  6. 为什么 String 类是不可变的?
  7. 我终于决定要放弃 Postman,选择了牛逼的神
  8. 使用后台进程和 Shutdown Hook 友好地关
  9. ConcurrentHashMap 和 HashTable 的区别
  10. 船新 IDEA 2020.3 插件大合集,个个都是精