Button实现一遍直角一边半圆和Button之间无距离

本文介绍了如何在Android中实现Button之间的无距离布局,通过设置`android:layout_marginLeft="-8dp"`来消除默认8dp的间距。同时,详细讲解了如何创建一个左边半圆右边直角或右边半圆左边直角的Button,包括使用不同的布局和选择器来达到所需效果。作者具有8年开发经验,提供技术支持和项目开发服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.Button之间无距离:当我们在xml中写两个Button的时候他们的实际距离是8dp;想让我们的Button之间无距离的话只要把距离设置为-8dp即可:android:layout_marginLeft="-8dp"

2.Button实现左边半圆右边直角或者右边半圆左边直角:

实现左边半圆右边直角的:国控

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 填充的颜色:这里设置为红色 -->
<solid android:color="#FF0000"/>
<!-- 设置按钮的四个角为弧形 android:radius 弧形的半径 -->
<corners android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:bottomRightRadius="0dp"
android:topRightRadius="0dp"/>
</shape>

实现中间的无直角:省控

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 填充的颜色:这里设置为红色 -->
<solid android:color="#FF0000"/>
</shape>

实现右边的左边直角右边圆角:小型站

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 填充的颜色:这里设置为红色 -->
<solid android:color="#FF0000"/>
<!-- 设置按钮的四个角为弧形 android:radius 弧形的半径 -->
<corners android:bottomLeftRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"/>
</shape>

选择器里面的状态在代码中已经介绍,在这里不再介绍,把xml中的整体效果展示一下:


<LinearLayout
android:id="@+id/ll_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="10dp"
android:orientation="horizontal">

<Button
android:id="@+id/tv_country"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="国控"
android:background="@drawable/horn_fragment_site"
android:textSize="25sp"/>

<Button
android:id="@+id/tv_province"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="省控"
android:background="@drawable/horn_fragment_site_mid"
android:layout_marginLeft="-8dp"
android:textSize="25sp"/>

<Button
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="小型站"
android:layout_marginLeft="-8dp"
android:background="@drawable/horn_fragment_right"
android:textSize="25sp"/>

</LinearLayout>

如果你想在代码中实现点击换颜色的话,你需要再写3中选择器,不过写的代码是同上的,这里不再介绍;只需要在监听的时候改变其他两个的状态即可!


本人具有8年开发经验(6年android开发),android技术支持、培训和小项目开发,联系qq:1157464105,价格保证最优惠

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值