看看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_width="wrap_content"  

android:layout_height="wrap_content"

android:text="TestShapeButton"  

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 渐变!描边!圆角!示例详解的更多相关文章

  1. 史上最易懂——ReactNative分组列表SectionList使用详情及示例详解

    React Native系列 <逻辑性最强的React Native环境搭建与调试> <ReactNative开发工具有这一篇足矣> <解决React Native un ...

  2. Spring Boot 2.x 快速入门(下)HelloWorld示例详解

    上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...

  3. Android 广播大全 Intent Action 事件详解

    Android 广播大全 Intent Action 事件详解 投稿:mrr 字体:[增加 减小] 类型:转载 时间:2015-10-20我要评论 这篇文章主要给大家介绍Android 广播大全 In ...

  4. jquery移除、绑定、触发元素事件使用示例详解

    这篇文章主要介绍了jquery移除.绑定.触发元素事件使用示例详解,需要的朋友可以参考下. unbind(type [,data]) //data是要移除的函数 $('#btn').unbind(&q ...

  5. gcc与g++的编译链接的示例详解

    一.编译方式的示例详解 1. 编译C代码 代码如下:main.c /*!  ************************************************************** ...

  6. Android EventBus 3.0 实例使用详解

    EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...

  7. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  8. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

  9. VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)

    步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...

  10. socket编程的同步、异步与阻塞、非阻塞示例详解

     socket编程的同步.异步与阻塞.非阻塞示例详解之一  分类: 架构设计与优化 简介图 1. 基本 Linux I/O 模型的简单矩阵 每个 I/O 模型都有自己的使用模式,它们对于特定的应用程序 ...

随机推荐

  1. echo 换行不换行

    echo换行输出需要转义符 -e 看以下例子: echo -e "It is the first line." >> a; echo -e "It is th ...

  2. 数据库管理员<三>

    数据库管理员  介绍 每个 oracle 数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个 dba 就够了,但是对于一个大的数据库 可能需要多个 dba 分担不同的管理职责.那么 ...

  3. android studio小技巧

    1. 为了防止乱码,请将 android studio 右下角编码设置成 UTF-8 2. Ctri + Q 查看api 3 Ctri + Shift +U 大小写互换 4 Ctrl + Alt + ...

  4. Problem 2136 取糖果---FUOJ (线段树+维护)

    http://acm.fzu.edu.cn/problem.php?pid=2136 题目大意: 给你n个袋子每个袋子里都装有糖果,然后呢你可以每次抽取一个连续的一个区间的袋子,然后带走里面最多糖果数 ...

  5. centos6.5中 nginx-1.6.3 编译安装

    参考来源:http://nginx.org/en/docs/configure.html nginx-1.6.3 编译安装:1) ./configure --help 查看编译选项 2) 需要安装一下 ...

  6. JS手机端去除默认自带的选择复制菜单

    在需要的div上添加以下控制-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: ...

  7. 第二章-如何使用github建立一个HelloWorld项目,git的add/commit/push/pull/fetch/clone等基本命令用法。--答题人:杨宇杰

    1.配置Git 首先在本地创建ssh 秘钥:在git bash输入: $ ssh-keygen -t rsa -C "your_email@youremail.com" eg:$ ...

  8. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. python 之 theano学习:

    (1)theano主要支持符号矩阵表达式 (2)theano与numpy中都有broadcasting:numpy中是动态的,而theano需要在这之前就知道是哪维需要被广播.针对不同类型的数据给出如 ...

  10. Win系统查看系统的几个命令

    1. 查看显卡或者显存信息 Win + R dxdiag 2. 查看显卡是独立显卡,或者集成显卡 查看计算机显卡的方法 第一种方法:查看主机连接显示器VGA线连接的接口. 如图:VGA连接线连接在主机 ...