Android自己定义矩形及selector、shape的使用
【声明】转载请注明出处。此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail
——每天写一篇博客。每天做一点技术积累!
Android自己定义矩形及selector、shape的使用
因为项目开发须要,曾经尽管用过selector、shape可是都没有好好去研究过,仅仅知道用。不知道它们的属性详细有哪些作用。
尽管网上一查就都知道了,感觉还是要自己去弄懂一下。
以下咱们一起去研究下:
一、xml布局文件
/測试Demo/res/layout/check_box.xml
<? xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:id="@+id/order_meth"
style="@style/style_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/selector_mine"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="送达方式"
android:textSize="18sp" /> <RadioGroup
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/sku_rd"
android:layout_marginLeft="20dp"
android:gravity="center"
android:checked="true"
android:padding="10dp"
android:text="一小时达" /> <RadioButton
android:id="@+id/rb_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/sku_rd"
android:layout_marginLeft="10dp"
android:gravity="center"
android:padding="10dp"
android:text="预定送达" />
</RadioGroup>
</LinearLayout> </LinearLayout>
二、点击时背景颜色处理
1、style样式
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="sku_rd">
<item name="android:textColor">@color/selector_text</item>
<item name="android:background">@drawable/check_boxs</item>
<item name="android:button">@null</item><!--去掉RadioButton的那个圆形-->
</style>
</resources>
2、点击RadioButton边框背景颜色变换的关键xml
属性解读
“true”表示按下状态使用(比如button按下)。“false”表示非按下状态使用。
android:state_focused="true/false"
“true”表示聚焦状态使用(比如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。
android:state_selected="true/false"
“true”表示选中状态使用(比如Tab打开);“false”表示非选中状态使用。
android:state_active="true/false"
“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。
(仅仅对能切换可勾选—非可勾选的构件实用。)
android:state_checkable="true/false"
“true”表示勾选状态使用。“false”表示非勾选状态使用。
android:state_checked="true/false"
true”表示勾选状态使用;“false”表示非勾选状态使用。
android:state_enabled="true/false"
“true”表示可用状态使用(能接收触摸/点击事件)。“false”表示不可用状态使用。
android:state_window_focused="true/false"
“true”表示应用程序窗体有焦点时使用(应用程序在前台);“false”表示无焦点时使用(比如Notification栏拉下或对话框显示)。
/測试Demo/res/drawable/check_boxs.xml
<? xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/shape_sku"></item>
<item android:state_pressed="true" android:drawable="@drawable/shape_sku"></item>
<item android:state_selected="true" android:drawable="@drawable/shape_sku"></item>
<item android:drawable="@drawable/shape_sku_normal"></item>
</selector>
3、文字颜色的变化
/測试Demo/res/color/selector_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 文字选中时的背景-->
<item android:state_focused="true" android:color="#DB251F"/>
<!-- 文字单击时的背景 -->
<item android:state_pressed="true" android:color="#DB251F"/>
<item android:state_checked="true" android:color="#DB251F"/>
<item android:state_selected="true" android:color="#DB251F"/>
<!-- 文字默认的背景 -->
<item android:color="#969696"/>
</selector>
三、以下是自己定义的两个矩形
属性解读
corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还能够把四个角设定成不同的角度。同一时候设置五个属性。则Radius属性无效
android:Radius="20dp" 设置四个角的半径
android:topLeftRadius="20dp" 设置左上角的半径
android:topRightRadius="20dp" 设置右上角的半径
android:bottomLeftRadius="20dp" 设置右下角的半径
android:bottomRightRadius="20dp" 设置左下角的半径
stroke:描边
android:width="2dp" 描边的宽度
android:color 描边的颜色。
我们还能够把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"
android:dashGap="3dp"
当中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。
solid:填充
android:color指定填充的颜色
/測试Demo/res/drawable/shape_sku_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1dp" android:color="#C8C8C8"/>
<corners android:radius="3dp"/>
<solid android:color="@android:color/white"/>
</shape>
/測试Demo/res/drawable/shape_sku.xml
<? xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="1dp" android:color="#DB251F"/>
<corners android:radius="3dp"/>
<solid android:color="@android:color/white"/>
</shape>
四、效果图
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
五、源代码地址点击打开链接
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处☞指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
Android自己定义矩形及selector、shape的使用的更多相关文章
- Android RadioGroup 及资源文件 & selector
RadioGroup :单选组 RadioButton :单选按钮 RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单 ...
- Android-----使用Button特效 selector+shape
当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等. 我们就以 ...
- android 自己定义控件
Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...
- Android菜鸟成长记9 -- selector的用法
在项目开发的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片.开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点 ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
- selector + shape
selector_shape.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...
- Android 自己定义控件实现刮刮卡效果 真的就仅仅是刮刮卡么
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40162163 , 本文出自:[张鸿洋的博客] 非常久以前也过一个html5的刮刮 ...
- Android自己定义控件皮肤
Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...
随机推荐
- Android-自己定义标题栏
Android-自己定义标题栏 2014年4月25日 分享知识点 最近也比較多事情,想发发博客就是心有余而力不足,本篇博文主要教大家怎样实现自己定义标题栏,非常easy.那么聪明的你一下就看懂. 有兴 ...
- C语言--指针(一)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- TeamTalk Android代码分析(业务流程篇)---消息发送和接收的整体逻辑说明
第一次纪录东西,也没有特别的顺序,想到哪里就随手画了一下,后续会继续整理- 6.2消息页面动作流程 6.2.1 消息页面初始化的总体思路 1.页面数据的填充更新直接由页面主线程从本地数据库请求 2.数 ...
- AlertDialog自己定义View的使用方法+怎样改变弹出框的大小
android系统定义了弹出框,支持我们自己定义布局: public AlertDialog getEditCustomDialog() { LayoutInflater inflater = get ...
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Unity3D脚本编程--基本概念
1. 简单介绍 在Unity3D中,游戏对象(GameObject)的行为是由附加其上的脚本来控制的,游戏开发人员通过编写脚本来控制游戏中的全部对象,如移动Camera等. GameObject能够被 ...
- UVA 11000- Bee 递推
In Africa there is a very special species of bee. Every year, the female bees of such species give b ...
- Redis学习笔记(二) Redis 数据类型
Redis 支持五种数据类型:string(字符串).list(列表).hash(哈希).set(集合)和 zset(有序集合),接下来我们讲解分别讲解一下这五种类型的的使用. String(字符串) ...
- [转]SQL Server 数据库规范
SQL Server 数据库规范 一. 命名规范常用对象命名规范,使用帕斯卡命名法(Pascal,单词首字母大写),统一使用英文. 1. 表.英文单数名词,尽量写完整单词名称一般不超过3个英文单词都可 ...
- WEB前端开发常用的优化技巧汇总
减少HTTP请求次数或者减少请求数据的大小 页面中每发送一次HTTP请求,都需要完成请求+响应这个完整的HTTP事务,会消耗一些时间,也可能会导致HTTP链接通道的堵塞,为了提高页面加载速度和运行的性 ...