Android自定义矩形及selector、shape的使用

【声明】转载请注明出处,此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail

——每天写一篇博客,每天做一点技术积累!

由于项目开发需要,以前虽然用过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”表示按下状态使用(例如按钮按下);“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>

四、效果图

五、源码地址点击打开链接

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处?指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 08:39:54

Android自定义矩形及selector、shape的使用的相关文章

android自定义样式大全:shape,selector,layer-list,drawable,动画,style

原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目录则新建一个,而不要将它放到drawable-hdpi等目录中.只需要在对应控件设置(bg_rectangle_with_stroke_dash.xml) android:background="@drawable/bg_rectangle_with_stroke_dash" 四种类型 使

Android自定义Shape的属性

Android xml资源文件中Shape的属性: solid 描述:内部填充 属性:android:color 填充颜色 size 描述:大小 属性: android:width 宽 android:height 高 gradient 描述:渐变色 属性: android:startColor渐变起始颜色 android:endColor渐变结束颜色 android:centerColor渐变中间颜色 android:angle 渐变的角度,angle=0时,渐变色是从左向右,然后逆时针方向转

Android自定义图形shape

在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大.另一种方式就是使用自定义图形来改变控件样式. 自定义图形shape有以下几种属性 1.solid:填充 2.gradient:对应颜色渐变. startcolor.endcolor就不多说了. android:angle 是指从哪个角度开始变 3.stroke:描边 4.corners:圆角 5.pad

Android样式的开发:selector篇

上一篇详细讲了shape的用法,讲解了怎么用shape自定义矩形.圆形.线形和环形,以及有哪些需要注意的地方.不过,shape只能定义单一的形状,而实际应用中,很多地方比如按钮.Tab.ListItem等都是不同状态有不同的展示形状.举个例子,一个按钮的背景,默认时是一个形状,按下时是一个形状,不可操作时又是另一个形状.有时候,不同状态下改变的不只是背景.图片等,文字颜色也会相应改变.而要处理这些不同状态下展示什么的问题,就要用selector来实现了. selector标签,可以添加一个或多个

Android 自定义ViewGroup之实现FlowLayout-标签流容器

本篇文章讲的是Android 自定义ViewGroup之实现标签流式布局-FlowLayout,开发中我们会经常需要实现类似于热门标签等自动换行的流式布局的功能,网上也有很多这样的FlowLayout,但不影响我对其的学习.和往常一样,主要还是想总结一下自定义ViewGroup的开发过程以及一些需要注意的地方. 按照惯例,我们先来看看效果图 一.写代码之前,有几个是问题是我们先要弄清楚的: 1.什么是ViewGroup:从名字上来看,它可以被翻译为控件组,言外之意是ViewGroup内部包含了许

Android自定义Button按钮显示样式 转http://my.oschina.net/amigos/blog/63009

首先写一个定义Button样式的XML文件: 新建Android XML文件,类型选Drawable,根结点选selector,文件名就buton_style吧 ? 1 2 3 4 5 6 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <i

浅谈Android样式开发之selector

引言 上一篇Android UI中文章我们详细介绍了Android中shape标签的使用.通过shape标签我们可以定义矩形.椭圆.环形.直线等效果.不过shape只能定义单一的形状,在实际开发中,我们经常需要有一些交互性的体验.例如,按钮按下时的效果,ListView中Item被选中时的样式.这些固然可以通过写Java逻辑代码来实现,但是Android系统为我们定义了selector标签来实现这些功能.这样一方面我们少写了些Java代码,同时selector定义在XML文件中,可维护性相比写在

android - 自定义(组合)控件 + 自定义控件外观

转载:http://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html android - 自定义(组合)控件 + 自定义控件外观 Android自定义View实现很简单 继承View,重写构造函数.onDraw,(onMeasure)等函数. 如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml.在其中定义你的属性. 在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://sc

(转载)Android自定义标签列表控件LabelsView解析

Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App,还是在前端的网页,我们经常会看到下面这种标签的列表效果: 标签列表 标签从左到右摆放,一行显示不下时自动换行.这样的效果用Android源生的控件很不好实现,所以往往需要我们自己去自定义控件.我在开发中就遇到过几次要实现这样的标签列表效果,所以就自己写了个控件,放到我的GitHub,方便以后使用.有