Android shape 渐变!描边!圆角!示例详解
Android 中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对shape有了大体的了解,稍作总结:
- 先看下面的代码:
<!-- 实心 -->
<solid android:color="#ff9d77"/> <!-- 渐变 -->
<gradient android:startColor="#ff8c00"
android:endColor="#FFFFFF" android:angle="270" />
<!-- 描边 --> <stroke
android:width="2dp" android:color="#dcdcdc" />
<!-- 圆角 --> <corners
android:radius="2dp" /> <padding
android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。
stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。
corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners
android:topRightRadius="20dp" 右上角
[/list]android:bottomLeftRadius="20dp" 右下角
android:topLeftRadius="1dp" 左上角
android:bottomRightRadius="0dp" 左下角
[list=1]
[align=left]/> [/align]
这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。
- padding:间隔
- 这个就不用多说了,XML布局文件中经常用到。
- 大体的就是这样,以下是一个使用的具体示例:用在Selector中作为Button的背景,分别定义了按钮的一般状态、获得焦点状态和按下时的状态,具体代码如下:
- main.xml:
<Button
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
/>
button_selector.xml:
[/align][/list][align=left]<?xml version="1.0" encoding="utf-8"?> [/align]<selector
[align=left] xmlns:android="http://schemas.android.com/apk/res/android"> [/align] <item android:state_pressed="true" >
[align=left] <shape>
[/align] <!-- 渐变 -->
[align=left] <gradient [/align] android:startColor="#ff8c00"
[align=left] android:endColor="#FFFFFF" [/align] android:type="radial"
[align=left] android:gradientRadius="50" />
[/align] <!-- 描边 -->
[align=left] <stroke [/align] android:width="2dp"
[align=left] android:color="#dcdcdc" [/align] android:dashWidth="5dp"
[align=left] android:dashGap="3dp" />
[/align] <!-- 圆角 -->
[align=left] <corners [/align] android:radius="2dp" />
[align=left] <padding [/align] android:left="10dp"
[align=left] android:top="10dp" [/align] android:right="10dp"
[align=left] android:bottom="10dp" />
[/align] </shape>
[align=left] </item> [/align] <item android:state_focused="true" >
[align=left] <shape>
[/align] <gradient
[align=left] android:startColor="#ffc2b7" [/align] android:endColor="#ffc2b7"
[align=left] android:angle="270" />
[/align] <stroke
[align=left] android:width="2dp" [/align]android:color="#dcdcdc" /> <corners
android:radius="2dp" /> [align=left]<padding [/align]android:left="10dp"
[align=left]android:top="10dp" [/align]android:right="10dp"
[align=left]android:bottom="10dp" /> </shape> [/align][align=left]</item> [/align]<item>
<shape>
<solid android:color="#ff9d77"/> [align=left]<stroke [/align]android:width="2dp"
[align=left]android:color="#fad3cf" />
[/align]<corners
[align=left]android:topRightRadius="5dp"
android:bottomLeftRadius="5dp" [/align][align=left]android:topLeftRadius="0dp"
android:bottomRightRadius="0dp" [/align][align=left]/>
[/align]<padding
[align=left]android:left="10dp" [/align]android:top="10dp"
[align=left]android:right="10dp" [/align]android:bottom="10dp" />
[align=left]</shape>
</item> [/align][align=left]</selector> [/align][list=1]
[align=left]
- 运行效果如下图:
- 一般状态:
- <IGNORE_JS_OP style="WORD-WRAP: break-word">

- 获得焦点状态:
- <IGNORE_JS_OP style="WORD-WRAP: break-word">

- 按下状态:
- <IGNORE_JS_OP style="WORD-WRAP: break-word">

Android shape 渐变!描边!圆角!示例详解的更多相关文章
- 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解
React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...
- Spring Boot 2.x 快速入门(下)HelloWorld示例详解
上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...
- Android 广播大全 Intent Action 事件详解
Android 广播大全 Intent Action 事件详解 投稿:mrr 字体:[增加 减小] 类型:转载 时间:2015-10-20我要评论 这篇文章主要给大家介绍Android 广播大全 In ...
- jquery移除、绑定、触发元素事件使用示例详解
这篇文章主要介绍了jquery移除.绑定.触发元素事件使用示例详解,需要的朋友可以参考下. unbind(type [,data]) //data是要移除的函数 $('#btn').unbind(&q ...
- gcc与g++的编译链接的示例详解
一.编译方式的示例详解 1. 编译C代码 代码如下:main.c /*! ************************************************************** ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- [置顶]
xamarin android toolbar(踩坑完全入门详解)
网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...
- VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)
步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...
- socket编程的同步、异步与阻塞、非阻塞示例详解
socket编程的同步.异步与阻塞.非阻塞示例详解之一 分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...
随机推荐
- Reflector 已经out了,试试ILSpy[转]
Reflector是.NET开发中必备的反编译工具.即使没有用在反编译领域,也常常用它来检查程序集的命名规范,命名空间是否合理,组织类型的方法是否需要改善.举例说明,它有一个可以查看程序集完整名称的功 ...
- HTTP-崔希凡笔记
HTTP协议(重点) 协议:协议的甲乙双方,就是客户端(浏览器)和服务器! 理解成双方通信的格式! l 请求协议: l 响应协议: 1 安装HttpWatch HttpWatch是专门为IE浏览器 ...
- memcache与memcached介绍及安装配置
也许大家一看到Memcache和Memcached会有点晕,这两者有什么关系又有什么区别呢,下面先给大家说下Memcached,Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应 ...
- 2016/10/28 很久没更了 leetcode解题 3sum
15. 3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- ICMP
(一)ICMP IP是一个尽力的不可靠的协议,IP不能提供差错控制(如果数据在传播过程中出现错误了),这个时候ICMP就起作用了. ICMP提供两个功能:差错的报告,查询. ICMP的ICMP包分为两 ...
- 使用12c的DBCA创建数据库的时候报错TNS-04404
情况:之前使用的默认的pdb数据库pdborcl,连接的时候使用SQLdeveloper,配置了tnsname.ora,里面添加了pdborcl的service:这里报错是感觉tnsname配置错误了 ...
- text-indent
<div class="top wd"> <div class="con fl "><a href="#"&g ...
- jsp 和jspf
http://www.cnblogs.com/liaojie970/p/5035077.html
- Titanium系列--安装Titanium Studio 中的Android SDK,JDK以及环境变量的配置(二)
Ubuntu安装配置JDK 1.先去 Oracle下载Linux下的JDK压缩包,我下载的是jdk-8u25-linux-x64.tar.gz文件,下好后直接解压 Step1:# 将解压好的jdk1. ...
- hadoop部署中遇到ssh设置的问题
尽管hadoop和一些培训视频课程上讲分布式部署比较详细,但是在部署时仍遇到了一些小问题,在此mark一下: 1.linux的namenode主机上安装了ssh,也启动了ssh,并且执行了: /etc ...