【声明】转载请注明出处。此文出自指尖飞落的博客: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. ubuntu中写一个shell脚本的过程

    gedit hello.sh ,然后输入 #!/bin/bash echo "Hello world!" chmod +x hello.sh ./hello.sh

  2. python 并发编程入门

    多进程 在Unix/Linux下,为我们提供了类似c中<unistd.h>头文件里的的fork()函数的接口,这个函数位于os模块中,相同与c中类似,对于父进程fork()调用返回子进程I ...

  3. 【解决】run-as: Package &#39;&#39; is unknown

    问题: [2014-07-30 20:20:25 - nativeSensorStl] gdbserver output: [2014-07-30 20:20:25 - nativeSensorStl ...

  4. SharePoint 2013 开启訪问请求

    1.通常,我们进入SharePoint 2013网站,假设没权限会提示该网站未被共享,而没有切换账号或者申请訪问,实在是非常流氓:事实上,SharePoint为我们提供了訪问请求页面.可是可能须要手动 ...

  5. comp.lang.javascript FAQ [zz]

    comp.lang.javascript FAQ Version 32.2, Updated 2010-10-08, by Garrett Smith FAQ Notes 1 Meta-FAQ met ...

  6. hdu 3074 求区间乘积

    线段树水题 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; ...

  7. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  8. USACO 2.1 Sorting a Three-Valued Sequence

    Sorting a Three-Valued Sequence IOI'96 - Day 2 Sorting is one of the most frequently performed compu ...

  9. Spark RDD概念学习系列之action操作

    不多说,直接上干货! action操作  

  10. MVC的一些常用特性,持续更新中。。。

    1. @MvcHtmlString.Create("<option value='1'>火星</option>")   //渲染Html