Android <shape>定义图形

shape标签可以用于自定义一些简单的图形

一个简单例子,定义一个圆角矩形:

在res/drawable目录下创建example.xml

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

    <corners android:radius="2dp" />

    <size android:height="20dp" android:width="30dp"/>

    <solid android:color="#ffffff"/>

    <stroke android:color="#ff6767" android:width="12dp"/>

</shape>

这里写图片描述
这就是定义好的圆角矩形

shape标签有一个android:shape属性,包括了四个值:

shapevalue
直线line
矩形rectangle
椭圆oval
圆环ring

这个属性定义了图形的类型。

在shape标签下还有五个标签:

标签内容属性
solid填充。直接填充颜色color: 填充的颜色
gradient渐变。指定多种颜色及渐变方式和角度,颜色渐变填充startColor: 起始颜色 centerColor: 中间颜色 endColor: 最终颜色 type: 渐变类型 angle: 角度……
corners圆角。比如矩形的圆角radius: 圆角半径 还有topLeft..、topRight、bottomLeft、bottomRight你懂的
padding边距left、right、top、bottom分别指哪里你懂的
size大小。指定形状的width和heightwidth和height也是你懂的
stroke描边。可以指定形状描边的线性、大小、颜色和间距等width: 描边线宽 color: 颜色 dashGap: 描边间隔长度 dashWidth: 描边间隔的宽度

然后就可以应用自定义的图形作为控件的背景等。

例如

将上述自定义的圆角矩形指定为布局容器的背景

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.easonchung.test.MainActivity">

    <!-- LinearLayout的background属性设置成刚刚定义的图形 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/example" 
        android:padding="16dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="30sp"/>

    </LinearLayout>

</RelativeLayout>

效果
效果如图

也可以用作按钮等控件的自定义样式

将example.xml复制一个副本命名为example_pressed.xml
然后将里面的solid颜色改为描边的颜色,而stroke颜色改为更鲜艳的红色

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

    <corners android:radius="2dp"/>

    <size
        android:width="30dp"
        android:height="20dp" />

    <solid android:color="#ff6767" />

    <stroke
        android:width="12dp"
        android:color="#ff2d2d" />

</shape>

再新建一个资源文件button.xml
这里需要用到 selector 标签

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

    <!-- 点击时的drawable -->
    <!-- 指定为example_pressed.xml -->
    <item android:drawable="@drawable/example_pressed" android:state_focused="true" />

    <!-- 点住不松手时的drawable -->
    <!-- 也指定为example_pressed.xml -->
    <item android:drawable="@drawable/example_pressed" android:state_pressed="true" />

    <!-- 一般时候的drawable -->
    <!-- 指定为example.xml -->
    <item android:drawable="@drawable/example" />

</selector>

之后再将Button的background指定为上面写好的button.xml

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:background="@drawable/button"/>

这里写图片描述
效果如图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值