【声明】转载请注明出处。此文出自指尖飞落的博客: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. hadoop无法启动DataNode问题

    因为种种原因,今天又一次安装hadoop集群.清空了/tmp下的文件夹,重新启动集群,hadoop namenode -format 之后  start-all   可是没有发现DataNode的守护 ...

  2. android 检測右滑的WebView

    今天产品出新花样非得要右滑....检測到右滑手势后事件不做处理放在Activity中做对应的处理即可了. import android.app.Activity; import android.con ...

  3. Android开发之控制手机音频

    本实例通过MediaPlayer播放一首音乐并通过AudioManager控制手机音频.关于AudioManager的具体解释可參照:Android开发之AudioManager(音频管理器)具体解释 ...

  4. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  5. nyoj--1185--最大最小值(线段树)

    最大最小值 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给出N个整数,执行M次询问. 对于每次询问,首先输入三个整数C.L.R: 如果C等于1,输出第L个数到第R个数 ...

  6. 【NOI 2002】 银河英雄传说

    [题目链接] https://www.luogu.org/problemnew/show/P1196 [算法] 并查集 [代码] #include<bits/stdc++.h> using ...

  7. Hadoop 三剑客之 —— 分布式文件存储系统 HDFS

    一.介绍 二.HDFS 设计原理     2.1 HDFS 架构     2.2 文件系统命名空间     2.3 数据复制     2.4 数据复制的实现原理     2.5 副本的选择     2 ...

  8. [jzoj 6093] [GDOI2019模拟2019.3.30] 星辰大海 解题报告 (半平面交)

    题目链接: https://jzoj.net/senior/#contest/show/2686/2 题目: 题解: 说实话这题调试差不多花了我十小时,不过总算借着这道题大概了解了计算几何的基础知识 ...

  9. vue-quill-editor 禁止编辑

    每天学习一点点,知识财富涨点点 因为权限问题需要对富文本vue-quill-editor进行禁止编辑,因为也不是专业前端,处理起问题来还是只有看文档和百度,发现对这个提问不是很多,可能很多大牛都不会出 ...

  10. HIT Software Construction Lab 2

    2019年春季学期 计算机学院<软件构造>课程 Lab 2实验报告 姓名 刘帅 学号 班号 1703008 电子邮件 1609192321@qq.com 手机号码 目录   1 实验目标概 ...