【声明】转载请注明出处。此文出自指尖飞落的博客: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. Android仿IOS的AssistiveTouch的控件EasyTouch实现

    概述: 之前我听到过一则新闻,就是说Ipone中的AssistiveTouch的设计初衷是给残疾人使用的. 而这一功能在亚洲(中国)的使用最为频繁. 虽不知道这新闻的可靠性,但无庸置疑的是它的确给我们 ...

  2. 使用gridlayout布局后,因某些原因又删除,并整理文件夹结构时,Unable to resolve target &#39;android-7&#39;

    出现的问题 [2013-01-11 10:52:39 - gridlayout_v7] Unable to resolve target 'android-7' 事由:在一次做九宫格时.误使用了gri ...

  3. MantisBT 问题分配显示 姓名

    MantisBT 在提交问题的时候,系统默认"分配"给备选账号,而不是姓名. 这样在使用的时候很不便. 能够通过改动配置文件来改变,找到MantisBT根文件夹下文件config_ ...

  4. VC 6.0中添加库文件和头文件 【转】

    本文转载自:http://blog.sina.com.cn/s/blog_9d3971af0102wxjq.html 加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools&g ...

  5. Swift - 获取应用名称、应用版本、设备型号、系统版本等信息

    有时我们在 App 中提交一些统计信息或者用户反馈信息时,为了能更好地进行分析,通常会附带上当前应用程序的名称.版本号.设备型号.以及设备系统版本.下面演示如何获取这些信息. 1,效果图 程序启动后自 ...

  6. hdoj--5532--Almost Sorted Array(正反LIS)

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  7. oracle得到建表语句

    第一种方法是使用工具,如:pl/sql developer,在[工具]--[导出用户对象]出现就可以得到建表脚本. 第二种方法是,sql语句. DBMS_METADATA.GET_DDL包可以得到数据 ...

  8. Java基础之对象序列化

    1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现 ...

  9. 如何使用github来展示自己的网页

    项目文档或者单纯的html页面怎么用github来展示呢? 第一步:新建库 第二步: 上传自己的页面(index.html需在根目录下) 先把git库克隆下来 进入lineShop文件夹,拷贝自己的页 ...

  10. [转]Linux常用命令学习

    转自 https://www.cnblogs.com/gaojun/p/3359355.html 1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文 ...