关于ShapeDrawable应用的一些介绍(上)
在Android中, 很多时候系统原生的控件的格式并不能满足我们的需求,我们想要更加好看点的样式,像什么圆角矩形啊,颜色渐变啊,阴影效果啊等等的,这个时候就是我们的 ShapeDrawable发挥效果的时候了,接下来我们这两篇文章就来说一下Shape的一些应用吧,掌握点基础知识,才能好好更好地去应用啊。
其实很多东西并不难,我们也不是不懂,但是关键得懂得总结呀,对吧。
1)首先,我们要在res/drawable/ 路径下创建一个xml文件,其格式如下(这是在Android的官方文档中拿出来的,我觉得真的很丰满,一目了然):
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape=["rectangle" | "oval" | "line" | "ring"] >
- <corners
- android:radius="integer"
- android:topLeftRadius="integer"
- android:topRightRadius="integer"
- android:bottomLeftRadius="integer"
- android:bottomRightRadius="integer" />
- <gradient
- android:angle="integer"
- android:centerX="integer"
- android:centerY="integer"
- android:centerColor="integer"
- android:endColor="color"
- android:gradientRadius="integer"
- android:startColor="color"
- android:type=["linear" | "radial" | "sweep"]
- android:useLevel=["true" | "false"] />
- <padding
- android:left="integer"
- android:top="integer"
- android:right="integer"
- android:bottom="integer" />
- <size
- android:width="integer"
- android:height="integer" />
- <solid
- android:color="color" />
- <stroke
- android:width="integer"
- android:color="color"
- android:dashWidth="integer"
- android:dashGap="integer" />
- </shape>
shape是根元素,其android:shape属性定义了这个xml文件定义的形状,可以是retangle,oval,line 和 ring。
corners(角)
- <corners
- android:radius="integer"
- android:topLeftRadius="integer"
- android:topRightRadius="integer"
- android:bottomLeftRadius="integer"
- android:bottomRightRadius="integer" />
一般是怎么应用的呢,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners android:radius="15dp"/>
- </shape>
2)在xml中定义按钮的时候,将其android:background设置成上面的corners文件,如下:
- <Button
- android:layout_margin="5dp"
- android:layout_width="200dp"
- android:layout_height="50dp"
- android:background="@drawable/corners"
- android:text="Corner" />
3)然后就可以了,不过我这里多定义了几个样式的,大家可以看着,对比一下,效果图如下:
Solid(填充)
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners android:radius="15dp"/>
- <solid android:color="#716283"/>
- </shape>
好,我们现在把填充的颜色给换了,不要黑色,再来看看效果吧。
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners android:radius="15dp"/>
- <solid android:color="#716283"/>
- </shape>
我们可以看到4个角都变成圆角了,说明android:radius属性是对四个角都起作用的。
- <corners android:topLeftRadius="15dp"/>
如果只定义topLeftRadius的话,则只有左上角会变成圆角,同理,topRightRadius也只会改变右上角。
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners
- android:radius="10dp"
- android:topLeftRadius="15dp"
- android:bottomRightRadius="15dp"/>
- <solid android:color="#716283"/>
- </shape>
则可以看到定义了radius的10dp就还是10dp,而topLeftRadius和bottomRightRadius,则会覆盖radius定义的10dp。
Padding
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle">
- <corners android:topLeftRadius="15dp"/>
- <solid android:color="#716283"/>
- <padding android:left="30dp"/>
- </shape>
在原来的基础上再加上padding效果,我们来看看吧。
关于ShapeDrawable应用的一些介绍(中)之Gradient
关于ShapeDrawable应用的一些介绍(上)的更多相关文章
- 关于ShapeDrawable应用的一些介绍(中)之Gradient
版权声明:本文为博主原创文章,未经博主允许不得转载. Gradient,渐变,是在界面设计中最经常用到的一种技巧,只要涉及到颜色的处理,浓妆淡抹总相宜,说的就是它. 在Android中,当然也提供了这 ...
- [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
最近在使用Python爬取网页内容时,总是遇到JS临时加载.动态获取网页信息的困难.例如爬取CSDN下载资源评论.搜狐图片中的“原图”等,此时尝试学习Phantomjs和CasperJS来解决这个问题 ...
- 【转载】Direct3D HLSL介绍(上)
原文路径:http://www.csharpwin.com/csharpspace/3087.shtml 写过Direct3D程序的朋友们可能还记得,在以往,大家常为如何表现更多真实的材质(如玻璃.金 ...
- Swift UI控件详细介绍(上)
UI控件 首先介绍一下AppDelegate.swift@UIApplicationMain 调用了OC中的UIApplicationMain函数:UIApplicationMain是iOS应用程序的 ...
- ASP.NET MVC 入门介绍 (上)
MVC模式 MVC模式是一种软件架构模式.它把软件系统分为三个部分:模型(Model),视图(View)和控制器(Controller).MVC模式最早由Trygve Reenskaug在1974年提 ...
- Android init介绍(上)
1. 介绍 init进程是Linux系统第一个用户进程,是Android系统应用程序的根进程,即1号进程(PID为1):Android中的init文件位于/init,代码位于system/core/i ...
- Bootstrap 学习笔记 项目实战 首页内容介绍 上
效果图: HTML代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset ...
- webpack介绍—上
6.1 webpack概念的引入 在网页中会引用哪些常见的静态资源? JS .js. .jsx ..coffee. .ts(TypeScript 类 C# 语言) CSS .css. .less. . ...
- webpack 项目接入Vite的通用方案介绍(上)
愿景 希望通过本文,能给读者提供一个存/增量项目接入Vite的点子,起抛砖引玉的作用,减少这方面能力的建设成本 在阐述过程中同时也会逐渐完善webpack-vite-serve这个工具 读者可直接fo ...
随机推荐
- 移动端弹性滑动以及vue记录滑动位置
-webkit-overflow-scrolling介绍 -webkit-overflow-scrolling: auto | touch; auto: 普通滚动,当手指从触摸屏上移开,滚动立即停止 ...
- JS排序之快速排序
JS排序之快速排序 一个数组中的数据,选择索引为(2/数组长度)的那个数据作为基数,数组中的其他数据与它对比,比它数值小的放在做数组,比它数值大的放在右数组,最后组合 左数组+基数+右数组,其中,左数 ...
- React+Dva
Reducer reducer 是一个函数,接受 state 和 action,返回老的或新的 state .即:(state, action) => state Effect app.mode ...
- 【PLSQL】游标
Oracle中的SQL在执行时需要分配一块内存区域,这块内存区域叫做上下文区. 上下文区中记录了SQL语句的处理信息,这些信息包括:查询返回的数据行.查询所处理的数据的行号.指向共享池中的已分析的SQ ...
- CentOS 7 中配置通过 daemon 模式启动的 Tomcat 8 服务
距离上次折腾已经有很长一段时间了... 不说这个,刚好有空闲,把这两天折腾的 Tomcat 8 的服务配置整理出来收录一下. 1.JDK安装 1)检查服务器是否预装了 openJdk,如果有就删除,在 ...
- Java_Jdbc_连接mysql数据库_1.打通数据库
准备工作:myeclipes,mysql,navicat,jar包等工具 首先,需要导入连接数据库需要的jar包.照着教程敲的程序一直出错,结果就是导jar包导得有问题. 正确的(不唯一)的步骤为:1 ...
- 小程序viewflex布局的对齐不对的问题
index.wxml: <view class="container"> <view class="nav-container"> &l ...
- json简介及josn数组中取字符
1.json字符串就是字符串,只不过格式是Json格式的,以键值对的形式存在,键和值可以是字符串,数字,空值,数组等. json对象在花括号中书写,一个json对象包含多个键值对,json对象以花括号 ...
- Java8新特性 Stream流式思想(二)
如何获取Stream流刚开始写博客,有一些不到位的地方,还请各位论坛大佬见谅,谢谢! package cn.com.zq.demo01.Stream.test01.Stream; import org ...
- 【maven】成功生成jar包,提示找不到主类?
问题描述: 使用maven构建zookeeper项目,完成一个简单的创建组的实例,代码调试完成,使用mvn clean install成功打包得到了jar包,但是在执行时发现使用java -cp ...