活用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和 ...
随机推荐
- centos5.11 repo 安装mysql5.7
http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html mysql yum repo 安装说明 http://d ...
- LogConfigruration
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import ...
- GNS3 IOU 配置
GNS3使用视频: http://edu.51cto.com/lesson/id-25295.html GNS3 IOU 与VM http://www.mamicode.com/info-detail ...
- 初始Java DVD项目
DVDSet 类: DVD DVD 删除功能 实现DVD借出功能 DVD还回功能
- Vertica并发DML操作性能瓶颈的产生与优化(转)
文章来源:中国联通网研院网优网管部IT技术研究团队 作者:陆昕 1. 引言 众所周知,MPP数据库以其分布式的超大存储能力以及列式的高速汇总能力,已经成为大数据分析比不可少的工具.Vertica就是这 ...
- MVC 路由介绍
我们新建一个ASP.NET MVC Web程序的时候,会生成一个Global.asax文件.如下: using System; using System.Collections.Generic; us ...
- Javascript学习笔记3 Javascript与BOM简介
什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...
- easyui的textbox赋值小结
使用的系统中有个后台,需要填充单号,如下图: 每次往框里面填充都是一样的数据,复制.粘贴,而且当人数颇多的时候,就是体力活. 于是就想到通过执行js代码,自动填充这些数据. chrome下F12,查看 ...
- Microsoft Help Viewer
Microsoft Help Viewer 1.0 Microsoft Help Viewer 2.0 Help Library 管理器 -Microsoft Help 查看器 打开VS 2010 ...
- POJ2774 (后缀数组)
#include<cstdio> #include<cstring> using namespace std; ],b[]; ],x[],wv[],ws[],h[],rank[ ...