Android shape和selector完全总结

shape和selector嵌套版
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic_red" />
< shape >
<!-- 渐变
type 的三种类型
sweep 点击后显示圆锥渐变
linear 显示线性渐变
radial 圆形渐变

gradientRadius 渐变扩散度
android:centerX="50%" 设置 x 轴中心偏移
android:centerY="50%" 设置 y 轴中心偏移
centerColor 中心颜色

angle 渐变旋转角度 ( 只能旋转 45 度的倍数 , 且不能用 type 中的 radial 渐变 )
solid 实心填充 , 有渐变就会被覆盖
size 调整大小 , 似乎没什么效果
-->
< solid android :color= "#0f0" />
< size android :height= "100dp" android :width= "100dp" />
< gradient
android :centerX= "50%"
android :centerY= "50%"
android :centerColor= "#ff39a9"
android :angle= "-45"
android :startColor= "#ff8c00"
android :endColor= "#0ad9d5"
android :type= "linear"
android :gradientRadius= "500" />
<!-- 描边
dashwidth 虚线边框线宽
dashgap 虚线边框空白宽度
-->
< stroke
android :width= "1dp"
android :color= "#fa46a6"
android :dashWidth= "30dp"
android :dashGap= "3dp" />
<!-- 圆角
可以设置单个角度的圆角
-->
< corners
android :radius= "20dp" />
<!-- 设置边距 -->
< padding
android :left= "0dp"
android :top= "0dp"
android :right= "0dp"
android :bottom= "0dp" />
</ shape >
</ item >
< item android :state_focused= "true" >
< shape >
< gradient
android :startColor= "#ffc2b7"
android :endColor= "#ffc2b7"
android :angle= "270" />
< stroke
android :width= "2dp"
android :color= "#dcdcdc" />
< corners
android :radius= "2dp" />
< padding
android :left= "10dp"
android :top= "10dp"
android :right= "10dp"
android :bottom= "10dp" />
</ shape >
</ item >
< item >
< shape >
< solid android :color= "#ff9d77" />
< stroke
android :width= "2dp"
android :color= "#fad3cf" />
< corners
android :topRightRadius= "5dp"
android :bottomLeftRadius= "5dp"
android :topLeftRadius= "0dp"
android :bottomRightRadius= "0dp"
/>
< padding
android :left= "10dp"
android :top= "10dp"
android :right= "10dp"
android :bottom= "10dp" />
</ shape >
</ item >
</ selector > shape单独使用
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 渐变
type的三种类型
sweep点击后显示圆锥渐变
linear显示线性渐变
radial圆形渐变

gradientRadius渐变扩散度
android:centerX="50%"设置x轴中心偏移
android:centerY="50%"设置y轴中心偏移
centerColor 中心颜色

angle 渐变旋转角度(只能旋转45度的倍数,且不能用type中的radial渐变)
solid 实心填充,有渐变就会被覆盖
size 调整大小,似乎没什么效果

-->
<solid android:color="#0f0" />
<size android:height="100dp" android:width="100dp"/>
<gradient
android:centerX="50%"
android:centerY="50%"
android:centerColor="#ff39a9"
android:angle="-45"
android:startColor="#ff8c00"
android:endColor="#0ad9d5"
android:type="linear"
android:gradientRadius="500" />
<!-- 描边
dashwidth 虚线边框线宽
dashgap 虚线边框空白宽度
-->
<stroke
android:width="1dp"
android:color="#fa46a6"
android:dashWidth="30dp"
android:dashGap="3dp" />
<!-- 圆角
可以设置单个角度的圆角
-->
<corners
android:radius="20dp" />
<!--设置边距-->
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>

 
  
这是单独使用selector
<?xml version="1.0" encoding="utf-8" ?>
 
  
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item android:drawable="@drawable/pic1" />

<!-- 没有焦点时的背景图片 -->
<item
android:state_window_focused="false"
android:drawable="@drawable/pic_blue"
/>

<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable= "@drawable/pic_red"
/>

<!-- 触摸模式下单击时的背景图片-->
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/pic_pink"
/>

<!--选中时的图片背景-->
<item
android:state_selected="true"
android:drawable="@drawable/pic_orange"
/>

<!--获得焦点时的图片背景-->
<item
android:state_focused="true"
android:drawable="@drawable/pic_green"
/>
</selector>

转载于:https://www.cnblogs.com/chenxuyuan/p/5084316.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值