活用shape、selector和layer-list来打造自己想要的背景效果
版权声明:本文为博主原创文章,未经博主允许不得转载。
我们都知道,Android中 一些控件默认的背景都比较难看,所以在大部分情况下,都需要我们自己用<shape>来进行一些美化效果,比如给button加个圆角,边线 之类的。当然假如想在点击的时候给一些反馈,我们还需要用到<selector>,再复杂一些的可能还需要我们用layer-list进行层 叠来实现。
下面我先说一下它们的简单用法(用法是网上找的,如有雷同,可能不是巧合~哈哈):
1.Shape
简介
作用:XML中定义的几何形状
Java代码中:R.drawable.文件的名称
<shape> Android:shape=["rectangle" | "oval" | "line" | "ring"]
<shape>中子节点的常用属性:
<gradient> 渐变
Android:startColor
Android:endColor
结束颜色
Android:angle
Android:type
<solid > 填充
Android:color
<stroke >描边
Android:width
Android:color
Android:dashWidth
表示'-'横线的宽度
Android:dashGap
<corners >圆角
Android:radius
Android:topRightRadius
Android:bottomLeftRadius
Android:topLeftRadius
Android:bottomRightRadius
<padding >填充
android:bottom="1.0dip"
android:left="1.0dip"
android:right="1.0dip"
android:top="0.0dip"
根据控件不同的选定状态来定义不同的显示效果
android:state_selected 是选中
android:state_focused 是获得焦点
android:state_pressed 是点击
android:state_checked 也是选中一般针对checkbox和radiobutton
android:state_window_focused 默认时的背景图片
引用位置:res/drawable/文件的名称.xml
使用的方法:
Java代码中:R.drawable.文件的名称
XML中:Android:background="@drawable/文件的名称"
3.layer-list
简介:可以将多个图片或上面两种效果按照顺序层叠起来,效果其实就和FrameLayout一样
用法其实都不难,关键是我们要学会灵活的运用到实际中,下面我举两个例子,来看一下具体怎么用。
1)实现效果如下:
效果比较常见,加减按钮在不点击的时候是黑色的,点击的时候变成橙色的。
我们先分析一下怎么做:
首先我们需要一个线性布局,里面放三个TextView(别的控件也可以)和两个view(黑色分割线),控件安排好了,接下来我们继续分析,最外
层的线性布局我们需要给弄一个圆角的效果,这个我们用<shape>就可以很容易做到,但是左边的TextView好像有点复杂,因为它既需
要圆角(左上和左下)又需要添加图片背景(减号)还需要根据状态发生变化,右边的也相同。那该怎么办呢,下面我贴一下代码:
布局中是这样子的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/shape_linear"
android:gravity="center_vertical"
android:orientation="horizontal"> <TextView
android:id="@+id/subtract"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tv_add"
android:gravity="center" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" /> <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="数量" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" /> <TextView
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/tv_subtract"
android:gravity="center"
android:selectAllOnFocus="true" />
</LinearLayout>
下面我们看一下shape_linear.xml、tv_subtract.xml、tv_add.xml:
shape_linear.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="5dp" />
<!--边线-->
<stroke
android:width="1dp"
android:color="#000000" /> </shape>
tv_subtract.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/subtract_selected" android:gravity="center"/> <shape > <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"/> </shape> </item> <item> <bitmap android:src="@drawable/subtract_no_select" android:gravity="center"/> <shape > <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"/> </shape> </item></selector>
tv_add.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/add_selected" android:gravity="center"/> <shape > <corners android:topRightRadius="5dp" android:bottomRightRadius="5dp"/> </shape> </item> <item> <bitmap android:src="@drawable/add_no_select" android:gravity="center"/> <shape > <corners android:topRightRadius="5dp" android:bottomRightRadius="5dp"/> </shape> </item></selector>
2)实现效果如下:
其实在这我就是举个例子,估计这样的需求不会有,上面要的结果是背景只显示三条边线,我们都知道<shape>的stroke属性就是
来设置边线的,可以它却没有办法针对特定的边来操作。那么这个应该怎么来实现呢,别忘了我们还有layer-list呢。我们都知道layer-list
其实就是将图层叠起来,想象一下,我们将一张大的图层放在第一层,然后我们再放一张小一点的图层盖在上面,那么第一层的边缘部分是不是还会漏在外面。好
了,看代码:
border_three.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--边线的颜色-->
<item>
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
<!--top left right 是偏移量-->
<!--主体的颜色-->
<item
android:left="1dp"
android:right="1dp"
android:top="1dp">
<shape>
<solid android:color="#dcdcdc" />
</shape>
</item>
</layer-list>
假如现在有个需求,是让你给按钮背景添加个阴影效果是不是也有思路了~
活用shape、selector和layer-list来打造自己想要的背景效果的更多相关文章
- Shape + Selector: Make a Shape as one item of the Selector
Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...
- android自定义样式大全:shape,selector,layer-list,style,动画全部内容
原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...
- 通过代码定义shape/selector
public class DrawableUtil { /** * 定义一个shape资源 * * @param rgb * @param corneradius * @return */ publi ...
- 【Android进阶学习】shape和selector的结合使用(转)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/732310 ...
- Android开发教程:shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- [Android UI] shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- 【Android进阶学习】shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- Android开发教程:shape和selector的结合使用(转载)
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
随机推荐
- ROS学习笔记(一)——软件版本的选择
下面是Google的SLAM系统Cartographer对系统的要求: Cartographer对ROS版本要求: ROS Indigo 对Ubantu 的版本要求: 所以,综上所述: Ubantu ...
- 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)
一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...
- 理解HTTP幂等性
转载: 理解HTTP幂等性 基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式.无论是在大型互联网应用还是企业级架构中,我们都见到了越来越多的SOA或RESTful的Web API ...
- JQuery中操作Css样式的方法
JQuery中操作Css样式的方法//1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#tw ...
- 【转】Python 日期和时间
本文转自:http://www.runoob.com/python/python-date-time.html Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Pytho ...
- 【burp】配置HTTPS抓包方法
以Chrome为例,配置HTTPS抓包方法 1.获取破解版的burp,将BurpLoader.jar和burpsuite_pro_v1.5.18.jar放到一个路径下 2.在cmd里进入上述两个jar ...
- web storage和cookie的区别
Web Storage的概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且每次你请求一个新的页面的时候Cookie都会被发送过去,这样无形中浪费了带宽,另外co ...
- Github windows客户端简单使用教程
1. 首先到官网下载Github客户端,官网地址:https://desktop.github.com/ 2. 点击上图红框的按钮开始下载客户端. 3. 双击下载好的客户端,开始安装. 双击之后出现一 ...
- 解决VS2015安装Android SDK 后文件不全及更新问题
近日安装VS2015专业版后.想进行Android开发,就新建了一个Blank app 结果报[值不能为空 null 参数名:path1] 1:首先检查工具 xamarin 工具那设置的SDK路径对不 ...
- iOs基础篇(二十二)—— UIPickerView、UIDatePicker控件的使用
一.UIPickerView UIPickerView是一个选择器控件,可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. 1.常用属性 (1)num ...