活用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和 ...
随机推荐
- Linq常用语法详细
1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...
- H5离线存储
如何使用 首先,我们建立一个html文件,类似这样: <!DOCTYPE html> <html lang="en" manifest="manifes ...
- php 二维数组排序
usort($info ,function($a,$b){ $a1 = $a['score']; $b1 = $b['score']; if($a1 == $b1) return 0; return ...
- MySQL使用技巧收集,持续更新中......
1.查询时按某一内容为中文的字段,以拼音字母排序: SELECT * FROM game ORDER BY CONVERT(name USING GBK);
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...
- Jquery想说爱你不容易
JQuery是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作.由John Resig在2006年1月的BarCamp NYC上发布第一个版本.目前是由 Dave M ...
- 转:spl_autoload_register与autoload的区别详解
转:http://www.poluoluo.com/jzxy/201306/209614.html spl_autoload_register(PHP 5 >= 5.1.2)spl_autolo ...
- sql sever读取Excel总结【转】
主要用到openrowset,opendatasource系统函数,这两个函数任意一个都能完成任务 用这种方法可以实现Excel和sqlserver表之间的相互导入导出. openrowset的写法 ...
- [原创]VM虚拟机Centos6.4网络配置。
关于虚拟机VMware 3种网络模式(桥接.nat.Host-only)的工作原理http://www.cnblogs.com/hehexiaoxia/p/4042583.html 操作环境 主机:W ...
- 立体角的单位——立体弧度(sr)
国际单位制(SI)中,关于物理量 发光强度 的介绍: 1cd(坎德拉)为一光源在给定方向的发光强度,该光源发出频率为540×1012Hz(赫兹)的单色辐射,且在此方向上的辐射强度为 1/683 W/s ...