android 用shape画任意一个边矩形边框

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--边框颜色-->
    <item>
        <shape>
            <solid android:color="@color/border_line" />
        </shape>
    </item>
    <!--    想保留哪一边就设置哪一边边框,列如只有下边框:bottom="0.5dp",相应的有上top="",left="",right=""    -->
    <!--整个框背景颜色-->
    <item android:bottom="0.5dp">
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</layer-list>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以使用 shape 资源来分别绘制四个边的边框。以下是使用 shape 绘制四个边的边框的步骤: 1. 创建一个 shape 资源文件,例如 border_shape.xml。在该文件中,使用 shape 标签定义形状和风格。为了分别绘制四个边的边框,可以使用 inset 标签来定义边框的位置和大小,例如: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/transparent" /> <inset android:insetLeft="1dp" android:insetTop="1dp"> <shape> <stroke android:width="1dp" android:color="#FF0000" /> </shape> </inset> <inset android:insetTop="1dp" android:insetRight="1dp"> <shape> <stroke android:width="1dp" android:color="#FF0000" /> </shape> </inset> <inset android:insetRight="1dp" android:insetBottom="1dp"> <shape> <stroke android:width="1dp" android:color="#FF0000" /> </shape> </inset> <inset android:insetBottom="1dp" android:insetLeft="1dp"> <shape> <stroke android:width="1dp" android:color="#FF0000" /> </shape> </inset> </shape> ``` 上述代码定义了一个矩形形状,四个边的边框宽度为 1dp,颜色为红色。使用 inset 标签来定义边框的位置和大小,例如 insetLeft 定义了左边框的宽度为 1dp,insetTop 定义了上边框的宽度为 1dp。 2. 在布局文件中,将对应的 View 的背景设置为该 shape 资源即可,例如: ``` <View android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/border_shape" /> ``` 这样,该 View 就会显示出一个四边分别绘制边框矩形形状。可以根据需要调整 shape 资源中的属性来修改边框的风格和形状。 注意:如果要在代码中动态设置边框,可以使用 setBackground 方法来设置 View 的背景。例如: ``` view.setBackground(ContextCompat.getDrawable(context, R.drawable.border_shape)); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值