android 水平平分两个按钮

项目中需要显示水平两个按钮,且都要有间距,如下图所示:


首先我想到的是使用权重,然后利用水平布局,这样应该可以实现,但真实的情况是这样的,代码如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >

 
    <View
        android:layout_width="wrap_content"
        android:layout_height="1.2px"
        android:layout_marginBottom="7dp"
        android:background="@color/white" />

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="79dp"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:layout_margin="10dp" >

        <Button
            android:id="@+id/bt1"
            android:layout_width="fill_parent"
            android:layout_height="26dp"
            android:background="@drawable/shape"
            android:layout_weight="1"
            android:text="確認對沖"
            android:textColor="@color/white"
            android:textSize="15dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="26dp"
            android:background="@drawable/shapeyuanjiao"
            android:layout_weight="1"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

显示的结果如下:


如果是正常的普通按钮,也确实能实现中间有一点空隙,并且水平均分,但对于我这个界面,都使用了圆角的效果,不知道什么原因,没有间距,所以又去查找百度,有一种解决方案是中间放一个隐藏的view,占据掉一些空间,不过这样尝试着不太好调,最后的解决方案其实是对于每一个按钮都给一个布局,这样就会存在空间,然后利用权重水平均分即可,文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical" >

    <View
        android:layout_width="wrap_content"
        android:layout_height="1.2px"
        android:layout_marginBottom="7dp"
        android:background="@color/white" />
<span style="font-size:18px;color:#ff0000;">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="79dp"
        android:layout_margin="10dp"
        android:layout_weight="2"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="79dp"
            android:layout_margin="10dp"
            android:layout_weight="2"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/bt1"
                android:layout_width="fill_parent"
                android:layout_height="26dp"
                android:layout_weight="1"
                android:background="@drawable/shape"
                android:text="確認對沖"
                android:textColor="@color/white"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="79dp"
            android:layout_margin="10dp"
            android:layout_weight="2"
            android:orientation="horizontal" >

            <Button
                android:layout_width="fill_parent"
                android:layout_height="26dp"
                android:layout_weight="1"
                android:background="@drawable/shapeyuanjiao"
                android:text="取消"
                android:textColor="@color/white"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout></span>

</LinearLayout>
运行结果如下:


成功解决。欢迎各位探讨布局。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值