Android中shape的使用

本人在美工方面一直是比较白痴的,对于一些颜色什么乱七八糟的非常头痛,但是在Android编程中这又是经常涉及到的东西,没办法,只有硬着头皮上。


Android中常常使用shape来定义控件的一些显示属性,今天看了一些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" />
        </shape>

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"    右上角
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上角
        android:bottomRightRadius="0dp"    左下角
 />
这里有个地方需要注意,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:
<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <!-- 渐变 -->
            <gradient
                android:startColor="#ff8c00"
                android:endColor="#FFFFFF"
                android:type="radial"
                android:gradientRadius="50" />
            <!-- 描边 -->
            <stroke
                android:width="2dp"
                android:color="#dcdcdc"
                android:dashWidth="5dp"
                android:dashGap="3dp" />
            <!-- 圆角 -->
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:startColor="#ffc2b7"
                android:endColor="#ffc2b7"
                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" />
        </shape>
    </item>

    <item>      
        <shape>
           
<solid android:color="#ff9d77"/>
            <stroke
                android:width="2dp"
                android:color="#fad3cf" />
            <corners
                android:topRightRadius="5dp"
                android:bottomLeftRadius="5dp"
                android:topLeftRadius="0dp"
                android:bottomRightRadius="0dp"
            />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

运行效果如下图:

一般状态:

 

获得焦点状态:

 

按下状态:

 

 

本文出自 “Kofi” 博客,请务必保留此出处http://kofi1122.blog.51cto.com/2815761/521605

转载于:https://www.cnblogs.com/xieyuan/p/3787488.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399786.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP遍历数组的几种方法

这三种方法中效率最高的是使用foreach语句遍历数组。从PHP4开始就引入了foreach结构&#xff0c;是PHP中专门为遍历数组而设计的语句&#xff0c;推荐大家使用。先分别介绍这几种方法 PHP中遍历数组有三种常用的方法&#xff1a; 一、使用for语句循环遍历数组&#xff1b; 二、…

Jmeter集合ant进行操作

1、下载ant包 地址【http://ant.apache.org/bindownload.cgi】 2、解压后&#xff0c;配置ant的环境变量&#xff0c;如下图 3、修改jmeter/extras中的build.xml的文件 代码如下&#xff1a; <?xml version"1.0" encoding"UTF-8"?><project nam…

五种常见的 PHP 设计模式

设计模式只是为 Java™ 架构师准备的 —— 至少您可能一直这样认为。实际上&#xff0c;设计模式对于每个人都非常有用。如果这些工具不是 “架构太空人” 的专利&#xff0c;那么它们又是什么&#xff1f;为什么说它们在 PHP 应用程序中非常有用&#xff1f;本文解释了这些问题…

jquery 点击事件

$("#id").click(function() {alert(alert);});

linux常见命令的常用方法示例

本文涉及命令&#xff1a;date,clock,hwclock,cal,ls,cd,pwd,tty,whereis,which,stat,echo,shutdown,halt,reboot,poweroff,who,w,whami部分命令结果等同&#xff0c;合到一起示例一、Date 打印或设置系统日期和时间1、date &#xff1a;查看当前系统日期和时间2、date %a:查看…

Leetcode: LRU Cache

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(k…

Day-17: 网络编程

---恢复内容开始--- 现有的互联网通讯方式&#xff0c;是服务器端的进程与客户端进程的通信。Python中进行网络编程&#xff0c;就是在Python程序本身这个进程内&#xff0c;连接别的服务器进程的通信端口进行通信。 互联网协议上包含了上百种协议标准&#xff0c;但是&#xf…

启动MySQL报错

当在linux系统中启动mysql服务的时候service mysqld start时 报&#xff1a;Another MySQL daemon already running with the same unix socket. 解决办法。 原因多个Mysql进程使用了同一个socket。 两个方法解决&#xff1a; 第一个是立即关机 使用命令 shutdown -h now 关机&…

计算机应用基础教程作业脑图 车辆工程学院 冯大昕

转载于:https://www.cnblogs.com/FengTang/p/7553055.html

UML该元素的行为为基础的元素

&#xfeff;&#xfeff;Behavioral thingsare the dynamic parts of UML models. These are the verbs of a model, representing behavior over time and space. In all, there are three primary kinds of behavioral things. 行为元件是UML模型的动态部分&#xff0e;它们…

EasyDSS高性能流媒体服务器前端重构(五)- webpack + vue-router 开发单页面前端实现按需加载 - 副本...

为了让页面更快完成加载, 第一时间呈现给客户端, 也为了帮助客户端节省流量资源, 我们可以开启 vue-router 提供的按需加载功能, 让客户端打开页面时, 只自动加载必要的资源文件, 当客户端操作页面, 切换功能模块, 触发页面路由变化时, 再去加载相应需要的资源. 本系列博客的前…

可工作的软件胜过面面俱到的文档

正在看一本书&#xff0c;书上讲到一个观点&#xff0c;“可工作的软件胜过面面俱到的文档”&#xff0c;这一点我一直都很认可&#xff0c;在平时工作中也是依据这一观点不写详细设计文档就开始编码&#xff0c;设计过程只做比较粗略的概要设计&#xff0c;但是涉及到数据库设…

解密阿里云七武器之高性能消息服务ONS

2019独角兽企业重金招聘Python工程师标准>>> 7月22日&#xff0c;首届阿里云分享日上&#xff0c;阿里云正式对外发布了企业级互联网架构解决方案&#xff0c;该服务由EDAS应用框架、ONS消息队列、DRDS分布式数据库组成&#xff0c;能有效解决企业上云后网站过载、性…

php动态数组的用法

// 创建连接 $con new mysqli($servername, $username, $password, $dbname); // Check conection if ($con->connect_error) {die("连接失败: " . $con->conect_error); }$sql "SELECT * FROM table limit 0,1000"; $result $con->query($sql…

MySQL划重点-查询-条件

查询的基本语法select * from 表名; 消除重复行在 select 后面列前使用distinct 可以消除重复的行 select distinct gender from students;一、条件 使用where子句对表中的数据筛选&#xff0c;结果为true的行会出现在结果集中语法如下 select * from 表名 where 条件;select *…

高数.........

高数70你敢信&#xff1f;&#xff1f;&#xff1f;临考前各种复习做卷子 各种认真 考试的时候感觉题目也都能做 尼玛连上平时分才给我70&#xff1f; 特么我是不信 打电话问问老师 如果没录错果断重修 郁闷转载于:https://www.cnblogs.com/ahu-shu/p/3520249.html

windows服务器下的ftp server搭建

软件下载链接&#xff1a;http://pan.baidu.com/s/1eQJbmUY ftpserver1.下载后打开。2.运行安装3.安装目录选择。这里我选择安装在C盘的FTP目录下&#xff0c;直接填写即可。这个安装目录可随意设置。4.安装启动。查看使用教程&#xff0c;添加用户名&#xff0c;设置密码&a…

WordPress获取当前分类ID的四种方法

WordPress获取当前分类ID的四种方法 时间: 2015-01-05 所属栏目: Wordpress教程 作者: WP管理员之家 关键词: wordpress,分类ID 关注热度&#xff1a; 4,346 次 (1条) WordPress获取当前分类ID的方法有很多&#xff0c;今天我来给大家总结一下吧,wordpress主题定制专家-WP管理…

linux笔记_20150825_linux下的软件工具唠叨下

这些都是书上看到的&#xff0c;有些工具我也没有完全用过。先记下来再说。闲着也是闲着。 1.linux下常见的语言及编程环境:c/c/java/perl/fortan等. 2.图形环境:gnome/kde/gimp/windowmaker/icewm等. 3.编辑器:xemacs/vim/gedit/pico等. 4.shells&#xff1a;bash/tcsh/ash/cs…

扩展编写jquery插件的方法

比如要扩展验证功能&#xff08;jquery.validate.js&#xff09;中的 messages: { required: "This field is required.", remote: "Please fix this field.", email: "Please enter a valid email address.", url: "Please enter a valid …