Android常用的shape,selector,layer-list使用说明

shape的优势,Android开发中,使用shape可以方便的帮我们画出背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适应不同的手机;1.shape(GradientDrawable)shape顾名思义就是形状的意思,我们在平时开发的中,应用的频率也很高,该文件是一个xml文件,并放在drawable文件夹下如res/drawable/filename.xml,那么引用方式也很简单,我们一般在控件的background使用,如android:background=.
摘要由CSDN通过智能技术生成

 shape的优势Android开发中,使用shape可以方便的帮我们画出背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适应不同的手机;

1.shape(GradientDrawable)

shape顾名思义就是形状的意思,我们在平时开发的中,应用的频率也很高,该文件是一个xml文件,并放在drawable文件夹下如res/drawable/filename.xml,那么引用方式也很简单,我们一般在控件的background使用,如android:background="@drawable/filename"

shape文件基本结构如下:

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

1.1<shape>标签下常见属性

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] >

属性说明

<!--   shape必须为根元素
xmlns:命名空间
android:shape指定形状类型
    rectangle:填充包含视图的矩形,当不写该属性时默认此形状
    oval:椭圆形状
    line:线形状,此形状需要 <stroke> 元素定义线宽
    ring:环形
当android:shape="ring"时,有可选属性
        android:innerRadius:尺寸。环内部(中间的孔)的半径
        android:innerRadiusRatio:浮点型。环内部的半径,以环宽度的比率表示。例如,如果 android:innerRadiusRatio="5",则内半径等于环宽度除以 5。此值被 android:innerRadius 覆盖。默认值为 9。
        android:thickness:环的厚度
        android:thicknessRatio:浮点型。环的厚度,表示为环宽度的比率。例如,如果 android:thicknessRatio="2",则厚度等于环宽度除以 2。此值被 android:innerRadius 覆盖。默认值为 3。
    需要注意的是,如果你使用了上面几个属性绘制一个圆,会发现在控件中并没有效果,我们还需要一个重要属性
        android:useLevel:布尔值。如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。
 -->
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]  />

1.2<corners>标签下常见属性

<corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />

属性说明

<!--
       corners为形状产生圆角,仅当形状为矩形时使用
        android:radius:尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。
        android:topLeftRadius:左上角的半径,以尺寸值或尺寸资源表示。
        android:topRightRadius:右上角的半径,以尺寸值或尺寸资源表示。
        android:bottomLeftRadius:左下角的半径,以尺寸值或尺寸资源表示。
        android:bottomRightRadius:右下角的半径,以尺寸值或尺寸资源表示
    -->

1.3<gradient>标签下属性说明

 <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />

属性说明

 <!--
        指定形状的渐变颜色,使用渐变时不要使用solid,否则会被覆盖,看不到效果
        android:angle:整型。渐变的角度(度)。0 为从左到右,90 为从下到上。必须是 45 的倍数。默认值为 0,如果写该值不是45的整数倍,将没有效果。
        android:centerX:浮点型。渐变中心的相对 X 轴位置 (0 - 1.0)。
        android:centerY:浮点型。渐变中心的相对 Y 轴位置 (0 - 1.0)。
        android:centerColor:颜色。起

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,<layer-list> 标签用于创建一个图形层列表,可以在一个图形中使用多个图层。每个图层可以指定颜色、形状、边框等属性,可以通过设置透明度来创建半透明效果。 <layer-list> 标签通常用于创建自定义的按钮、标签等 UI 控件,也可以用于创建渐变背景、圆形头像等效果。 以下是一个使用 <layer-list> 标签创建自定义按钮的示例: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <layer-list> <item> <shape android:shape="rectangle"> <corners android:radius="8dp" /> <solid android:color="#333333" /> </shape> </item> <item android:bottom="4dp"> <shape android:shape="rectangle"> <corners android:radius="8dp" /> <solid android:color="#555555" /> </shape> </item> </layer-list> </item> <item> <shape android:shape="rectangle"> <corners android:radius="8dp" /> <solid android:color="#444444" /> </shape> </item> </selector> ``` 该示例使用 <layer-list> 标签定义了两个图层,第一个图层为按钮的底部,使用矩形形状和圆角边框,填充颜色为 #333333;第二个图层为按钮的顶部,也使用矩形形状和圆角边框,填充颜色为 #555555,并设置了 bottom 属性为 4dp,使其相对于底部图层向上偏移 4dp。当按钮被按下时,会显示底部图层和顶部图层的组合,形成按下效果。未被按下时,只显示底部图层,形成普通状态的按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值