Android中shape和selector的使用详解

shape的基本用法就是在res/drawable下定义一个xml文件用于定义shape,然后在布局控件中如Button,TextView等中用background使用shape。

先了解shape的基本属性,准备基础知识:

1、shape:可以设置的形状:rectangle:矩形, 默认为矩形;oval:椭圆;line:线;ring:环形;

2、corners:弧度,可以设置半径或者圆角,radius半径,android:topLeftRadius:整型 左上角半径,android:topRightRadius:整型 右上角半径;android:bottomLeftRadius:整型 左下角半径,android:bottomRightRadius:整型 右下角半径;

3、 size:设置的大小,宽和高;

4、solid:内部填充色;

5、padding:内部间距,通过设置bottom,left,right,top;

6、stroke:设置边框,属性有dashWidth:虚线的长度,dashGap:虚线之间的距离,width宽度,color颜色;

7、gradient:颜色渐变,通过设置 android:startColor,android:centerColor,android:endColor,android:angle整型,渐变角度,当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍。

下面看下使用效果

1、在res/drawable文件夹下创建一个名为tv_shape_demo的xml文件:

<?xml version= "1.0" encoding ="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
    >
    <!-- corners : 弧度 -->
    <corners android:radius="16dp"/>
    <!--  size : 设置大小-->
    <size
        android:width="200dp"
        android:height="32dp"
        />
    <!-- solid : 颜色 -->
    <solid android:color="#993bc4c9"/>

    <!-- 颜色渐变 -->
    <!-- <gradient android:startColor="#ff0000"  android:centerColor="#00ff00"  android:endColor="#0000ff" android:angle="-90"/> -->

    <!-- 内部间距 -->
    <padding
        android:bottom="10dp"
        android:left="30dp"
        android:right="30dp"
        android:top="10dp"/>
    <!-- 设置边框 dashWidth : 虚线的长度   dashGap:虚线之间的距离-->
    <stroke
        android:width="5dp"
        android:color="#de151f"
        android:dashGap="5dp"
        android:dashWidth="5dp"/>

</shape>
2、布局文件中设置background:

<TextView
        android:gravity="center"
        android:background="@drawable/tv_shape_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
3、看下效果,为了展现属性效果,略丑

另外,补充下selector的用法:

1、shape加载到selector中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/button"/>
</selector>
TextView、ImageView、RelativeLayout等控件默认没有点击效果,需要加上android:clickable="true"属性或者加上点击事件后才有点击效果 

2、使用color定义selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<!-- 不能直接使用颜色值,需要间接引用 -->
	<item android:drawable="@color/primary_color" android:state_pressed="true"/>
	<item android:drawable="@color/secondary_color"/>
</selector>

3、多个条件的Selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 获得焦点并且可用时的图片,是且的关系 -->
    <item android:drawable="@drawable/blacklist_edit_edittext_bg_focused" android:state_focused="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/blacklist_edit_edittext_bg_unenable"/>
</selector>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值