设置控件的背景ShapeDrawable的使用(可设置按钮为圆角或者制定某个角为圆角)

本文介绍了如何使用Shape Drawable在Android中创建自定义背景,包括设置矩形或椭圆形背景,并重点讲解了如何指定边角半径实现圆角或特定角的圆角效果。同时,文章还涵盖了渐变、描边和填充等属性的使用,通过实例展示如何创建具有不同视觉效果的背景形状。

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

先看一下文档对Shape Drawable的描述:

Shape Drawable

An XML file that defines a geometric shape, including colors and gradients.

Creates aShapeDrawable. 一个定义几何形状的XML文件,包括颜色和渐变。创建一个ShpeDrawable对象

用android:background="@drawable/xxx.xml"或相应的Java代码引用,Shape Drawable说白了就是可自定义的多样化的背景。

现在对比较重要的标签和属性进行解释:

和其它drawable文件一样,在drawable目录下创建xml文件,以<shape>为根元素,有两个重要属性

1、xmlns:android="http://schemas.android.com/apk/res/android" 这个不说了,必填

2、android:shape 定义shape的形状有4个值"rectangle","oval","line","ring"分别是矩形、椭圆、线型和环形,一般用的较多的是前两种

shape元素下的其它属性均仅适用于android:shape="ring"时。

Shape的子元素包括

<corners>

<gradient>

<padding>

<size>

<solid>

<stroke>

<corners>指定边角的半径,简单的说,数值越大角越圆,数值越小越趋近于直角,参数为:

1. android:radius="integer"

2. android:topLeftRadius="integer"

3. android:topRightRadius="integer"

4. android:bottomLeftRadius="integer"

5. android:bottomRightRadius="integer"

1. android:angle="integer" 设置渐变的角度,数值必须为45的倍数,默认为0,即从

左到右渐变。当值为90时,从下到上渐变,以此类推当值为180时

2. 为270时从上向下渐变。

3. android:centerX="integer" 渐变的水平中心点,值为0-1.0

4. android:centerY="integer" 渐变的垂直中心点,值为0-1.0

5. android:centerColor="integer" 中心的颜色,非必须

6. android:endColor="color" 结束渐变时的颜色

7. android:gradientRadius="integer" 渐变的半径,仅适用于android:type="radial"

8. android:startColor="color" 开始渐变时的颜色

9. android:type=["linear" | "radial" | "sweep"] 渐变的类型,分别为线状、中心放

射状、经测试类似于雷达扫描的一种形态(词汇匮乏不知道该怎么翻译,⊙﹏⊙b汗)

10. android:usesLevel

=["true" | "false"] 貌似是可以被当做level-list用的意思

第2 / 4页

<padding>是为内容或子标签设置边距,4个属性top、bottom、left、right,需要注意的是这个标签的作用是为内容设置与当前应用此shape的View的边距,而不是设置当前View与父元素的边距。

<size>就不用说了,设置背景大小,width和height俩属性。 <solid>只有一个属性,即color,设置整个背景的颜色,如果已经定义了渐变色,那么这个属性就无效了。

<stroke> 描边,即设置边框

[html] view plaincopyprint? 1. <span style="font-size:16px;"> android:width="integer" 描边的宽

2. android:color="color" 描边的颜色

3. android:dashWidth="integer" 必须与下面的dashGap搭配使用缺一不可,也

就是说可以两个属性都不用,如果用,两个都必须得用。那么这哥俩有什么作用呢?

4. 原来是可以实现边框分段的效果,前者指定每段的长度后者指定两端之间的间隙或者说距

离。

5. android:dashGap="integer" 上图说明,如下,灰色部分为背景,黄绿色部分

是边框(好恶心的颜色啊,我随便填了几个数出来的)</span>

实例:抽选一部分常用的演示:

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


    <!-- 定义四个角的弧度  -->
    <corners
        android:bottomLeftRadius="8dip"
        android:bottomRightRadius="8dip"
        android:topLeftRadius="8dip"
        android:topRightRadius="8dip" />
    <!-- 定义背景颜色 -->
    <solid android:color="#990008" />
    <!-- 设置颜色渐变 -->
    <gradient
        android:angle="270"
        android:endColor="#FFFF00"
        android:startColor="#8BD71E" />
    <!-- 设置边框 -->
    <stroke android:width="3dip"
        android:color="#8BD71E"
        />
</shape>

效果如图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值