shape使用

本文介绍了如何使用 Android 的 Shape XML 来定义各种图形样式,包括直线、虚线、线性渐变、圆角实心及空心边框等,并提供了具体的 XML 示例代码。

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

shape属性:

Shape的属性(rectangle、oval、line、ring)
Shape自已是可以定义当前Shape的形状的,矩形,椭圆形,线形和环形;这些都是通过Shape标签的 shape属性来定义的。默认是rectangle

shape下面 一共有6个子节点, 常用的 就只有 四个,padding 和size 一般用不到。

corners ———-圆角
gradient ———-渐变
padding ———-内容离边界距离
size ————大小 
solid  ———-填充颜色
stroke ———-描边

定义直线:
参考链接 http://blog.youkuaiyun.com/piglovesula/article/details/24318739

shape_line

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
        <stroke
            android:width="2dp"
            android:color="#ff0000" />
        <size android:height="0.5dp" />
    </shape>

使用:

    <View
        android:layout_width="50dp"
        android:layout_height="3dp"
        android:background="@drawable/shape_line" />

定义虚线:
shape_dash_line

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

    <gradient
        android:endColor="#FFFFFF"
        android:startColor="#000000"
        android:type="linear" />
    <stroke
        android:width="2dp"
        android:color="#ff0000"
        android:dashGap="2dp"
        android:dashWidth="2dp" />
    <size android:width="20dp" />

</shape>

使用: 加上这一句才有效 android:layerType=”software”,不然显示的是直线

  <View
    android:layout_width="50dp"
    android:layout_marginTop="30dp"
    android:layout_height="5dp"
    android:layerType="software"
    android:background="@drawable/shape_dash_line"
   />

定义线性渐变:

shape_gradual_line

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

    <gradient
        android:endColor="#FFFFFF"
        android:startColor="#000000"
        android:type="linear" />
    </shape>

使用 height=1dp是直线,height大于1dp是线性渐变的矩形

    <View
        android:layout_width="50dp"
        android:layout_height="1dp"
        android:background="@drawable/shape_gradual_line" />

定义圆角实心:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <corners android:radius="20dp"/>
    <solid android:color="#ff0000"/>
</shape>

定义圆角空心边框背景图:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp" />
    <solid android:color="#ffffff" />
    <stroke
        android:width="2dp"
        android:color="#ff0000" />
</shape>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz白龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值