Android Shape的使用

应用场景:

在Android中常常会使用shape来定义控件的一些显示属性,例如背景颜色、背景圆角、背景渐变等效果。

使用方式:

第一步:在工程的res目录下新建drawable文件夹(若已存在无需新建),在drawable文件夹下新建Android XML File文件shape00.xml。

第二步:shape00.xml中填写文件内容,相关各个属性的作用与可选内容介绍如下。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <!-- 圆角 -->
    <corners
        android:radius="7dp" 
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp">
        <!-- 
        android:radius 设置角的弧度,值越大角越圆 
        -->
    </corners>
    <!-- 渐变 -->
    <gradient
        android:startColor="#123456"
        android:centerColor="#234567"
        android:endColor="#345678"
        android:useLevel="true"
        android:angle="0"
        android:type="linear"
        android:centerX="1%"
        android:centerY="3%"
        >
        <!-- 
                     当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效
        android:angle       渐变角度(PS:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往

上。angle必须为45的整数倍)  
        android:type        渐变类型(取值:linear、radial、sweep)  
                            linear  线性渐变,这是默认设置  
                            radial  放射性渐变,以开始色为中心。  
                            sweep   扫描线式的渐变。  
        android:centerColor 渐变中间颜色,即开始颜色与结束颜色之间的颜色  
        android:useLevel    如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。false有渐变色  
        android:gradientRadius  渐变色半径.当 android:type="radial" 时才使用。单独使用 android:type="radial"会报

错。  
        android:centerX     渐变中心X点坐标的相对位置  
        android:centerY     渐变中心Y点坐标的相对位置  
         -->
    </gradient>
    <!-- 间隔 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp">
    </padding>
    <!-- 大小 -->
    <size
        android:width="134dp"
        android:height="36dp">
        
    </size>
    <!-- 填充 -->
    <solid
        android:color="#567846">
        
    </solid>
    <!-- 描边 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
        android:dashWidth="2dp"
        android:dashGap="12dp">
        <!-- 
        dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框
		android:width="20dp"                  设置边边的宽度 
		android:color="@android:color/black"  设置边边的颜色 
		android:dashWidth="2dp"               设置虚线的宽度 
		android:dashGap="20dp"                设置虚线的间隔宽度
        -->
    </stroke>
</shape>

android:background="@drawable/shape00"
页面效果:效果截图  仅供演示







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hoking

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

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

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

打赏作者

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

抵扣说明:

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

余额充值