Android XML——shape标签

这篇博客详细介绍了如何在Android中使用XML的shape标签来创建不同形状,包括矩形、椭圆、线和环形,并探讨了shape的各种属性,如填充、渐变、圆角、描边和尺寸设置。通过配合selector使用,shape还能实现动态效果。同时,文中提到了绘制虚线的注意事项,包括线宽、居中显示和层类型设置。

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

创建:在res/drawable下创建:sha_btn.xml
使用:在XML中
android:background="@drawable/sha_btn"
配合selector使用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/sha_btn" android:state_activated="false" />
    <item android:drawable="@drawable/sha_btn" android:state_activated="true" />
</selector>

shape可以自定义形状,通过android:shape指定以下四种形状:
1.rectangle: 矩形,默认的形状,可以画出直角矩形、圆角矩形、弧形等
2.oval: 椭圆形,用得比较多的是画正圆
3.line: 线形,可以画实线和虚线
4.ring: 环形,可以画环形进度条

在shape根属性中包含有以下属性:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="false|true"             //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动。默认值为 true。
    android:shape="rectangle|line|oval|ring"//分别为矩形、线、椭圆、环。默认为矩形rectangle
    android:innerRadius="integer"           // shape为ring时可用,内环半径
    android:innerRadiusRatio="float"        // shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖
    android:thickness="integer"             // shape为ring时可用,环的厚度
    android:thicknessRatio="float"          // shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
    android:tint="color"                    // 给shape着色
    android:tintMode="src_in|src_atop|src_over|add|multiply|screen" // 着色类型
    android:useLevel="false|true"           // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
    android:visible="false|true" 
    >
</shape>

关于着色的内容可以参考这个链接:https://blog.youkuaiyun.com/u010687392/article/details/47399719

其他属性:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid 	/>
    <padding 	/>
    <gradient	 />
    <corners 	/>
    <stroke 	/>
    <size 	/>
</shape>

1.solid:填充,只有android:color一个属性,用于设置shape填充的颜色
2.padding:内边距,可设置左右上下的距离:

    android:left=""         <!--左内间距-->
    android:top=""          <!--右内间距-->
    android:right=""        <!--上内间距-->
    android:bottom="" />    <!--下内间距-->

3.gradient:设置渐变颜色

android:type 指定渐变的类型:linear 线性渐变,默认的类型;radial 放射渐变;sweep 扫描性渐变
android:startColor 渐变开始的颜色
android:endColor 渐变结束的颜色
android:centerColor 渐变中间的颜色
android:angle 渐变的角度,线性渐变时才有效,必须是45的倍数,0表示从左到右,90表示从下到上
android:centerX  渐变中心的相对X坐标,放射渐变才有效,在0.0到1.0之间,默认为0.5,表示在正中间
android:centerY 渐变中心的相对Y坐标,放射渐变才有效,在0.0到1.0之间,默认为0.5,表示在正中间
android:gradientRadius 渐变的半径,只有放射渐变时才使用
android:useLevel 如果为true,则可在LevelListDrawable中使用

类型
图片来源:https://www.cnblogs.com/MianActivity/p/5867776.html

4.corners:设置圆角

android:radius 圆角半径,会被下面每个特定的圆角属性覆盖
android:topLeftRadius 左上角的半径
android:topRightRadius 右上角的半径
android:bottomLeftRadius 左下角的半径
android:bottomRightRadius 右下角的半径

5.stroke:描边边框,可实线虚线

android:color 描边的颜色
android:width 描边的宽度。
android:dashWidth 设置虚线时的横线'-'的长度,值为0时,表示为实线;值大于0则为虚线。  
android:dashGap 描边为虚线时,两个横线'-'之间的距离

6.size:设置宽度和高度,相当于view的android:width和android:height。

<size
        android:width="100dp"
        android:height="100dp" />

<Button
            android:id="@+id/"
            android:layout_width=""
            android:layout_height="" 
            android:width=""
            android:height=""
            />

只有android:layout_width和android:layout_height都设置wrap_content后才会生效,原因如下:

首先我们应该知道一个控件的大小并不是由它自己本身来决定的,而是由父布局和它自身一起来决定的。
而layout_width就是父布局允许view所占的宽度,而width是view的自身宽度。在layout_width和width都设置为
具体数值的时候,width其实就无效了,这也不难理解,父布局已经给你分配了具体的空间,
不论view怎么设置width,view的宽度也只能是layout_width.那么什么情况下,width也会起作用呢。
当我们把layout_width设置成wrap_content的时候,父布局的意思是包裹view,view有多大就分配多大空间给它,
这时候view的宽度就取决于width,假如不设置width,那么系统就会根据view的内容来自行测量大小。
layout_height和height是一样的。

画虚线时,有几点特性必须要知道的:
只能画水平线,画不了竖线;
线的高度是通过stroke的android:width属性设置的;
线在整个形状区域中是居中显示的;
线左右两边会留有空白间距,线越粗,与stroke的dashWidth的值相关。
引用虚线的view必须添加属性android:layerType,值设为"software",否则显示不了虚线。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:color="@color/colorAccent" android:width="1dp" android:dashGap="4dp" android:dashWidth="4dp"></stroke>
</shape>
在布局文件里面添加View控件
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@drawable/"
    android:layerType="software"></View>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值