【声明】转载请注明出处。此文出自指尖飞落的博客: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的使用的更多相关文章

  1. Android RadioGroup 及资源文件 & selector

    RadioGroup :单选组         RadioButton :单选按钮 RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单 ...

  2. Android-----使用Button特效 selector+shape

    当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等. 我们就以 ...

  3. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  4. Android菜鸟成长记9 -- selector的用法

    在项目开发的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片.开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点 ...

  5. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  6. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  7. selector + shape

    selector_shape.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...

  8. Android 自己定义控件实现刮刮卡效果 真的就仅仅是刮刮卡么

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40162163 , 本文出自:[张鸿洋的博客] 非常久以前也过一个html5的刮刮 ...

  9. Android自己定义控件皮肤

    Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...

随机推荐

  1. 菜鸟的mongoDB学习---(六)MongoDB 索引

    MongoDB 索引 ps:大概有半个月木有更新了,因为前一阶段的出差和这几天突然来的项目.导致上网时间急剧降低,实在是sorry,以后预计会好一点. 索引通常可以极大的提高查询的效率.假设没有索引. ...

  2. CF 372B Counting Rectangles is Fun [dp+数据维护]

    题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 watermark/2/text/aHR0cDovL2Jsb2 ...

  3. xhprof安装&amp;&amp;使用

    听同事说起过一个php性能分析扩展,叫xhprof,近期了解了下. XHProf 是一个轻量级的分层性能測量分析器. 在数据收集阶段.它跟踪调用次数与測量数据,展示程序动态调用的弧线图. 它在报告.后 ...

  4. NESTED LOOPS & HASH JOIN & SORT MERGE JOIN

    表连接方式及使用场合 NESTED LOOP 对于被连接的数据子集较小的情况,nested loop连接是个较好的选择.nested loop就是扫描一个表,每读到一条记录,就根据索引去另一个表里面查 ...

  5. 剑指offer——01二维数组中的查找(Python3)

    题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  6. Python的四个内置数据类型list, tuple, dict, set

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

  7. vue项目中遇到的打印,以及处理重新排版后不显示echarts图片问题。

    1. 项目中用到的打印 页面: css: 控制好宽度一般A4 我调试的是794px多了放不下,小了填不满.当时多页打印的时候,一定要控制好每一个页面内容显示的高度不要超过一个页面,当然根据自己项目来. ...

  8. Spark RDD概念学习系列之如何创建Pair RDD

    不多说,直接上干货! 创建Pair RDD Python语言 pairs = lines.map(lambda x: (x.split(], x))  scala语言 val pairs = line ...

  9. 20个非常有用的Java程序片段--转

    原文地址:http://geek.csdn.net/news/detail/236591 下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = ...

  10. Winform WPF 窗体显示位置

    WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...