关于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>
- <Button
- android:layout_margin="5dp"
- android:layout_width="200dp"
- android:layout_height="50dp"
- android:background="@drawable/corners"
- android:text="Corner" />
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>
- <corners android:topLeftRadius="15dp"/>
- <?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>
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>