用Drawable来实现UI元素的间隔

一、在LinearLayout里面设置3个Button

<LinearLayout
        android:id="@+id/buttons_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_first"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="#903212"
            android:text="button_1" />

        <Button
            android:id="@+id/btn_second"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="#435283"
            android:text="button_2" />

        <Button
            android:id="@+id/btn_third"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#648205"
            android:text="button_3" />
    </LinearLayout>

效果图为:



二、将Button3的visibility属性设置为gone,效果为


可以看出Button2的右边有间距,需要实现的是Button2右边紧挨屏幕


三、通过Linearlayout的divider属性来实现

在drawable目录下创建spacer_mudium.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="10dp"
        android:height="10dp" />

    <solid android:color="@android:color/transparent" />

</shape>

在LinearLayout设置showDividers和divider属性





需要注意的:LinearLayout的divider属性是API 11 才加入的,如果要在低版本使用divider属性则需要用LinearLayoutCompat.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值