关于Shape使用的官方文档:
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Android中常常使用shape来定义控件的一些显示属性,下面是Shape中的所有属性及一个简单的示例:
- <?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>
下面是一个小示例:
main.xml文件内容如下:
- <p><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:paddingLeft="40dip" ></p><p> <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/button_selector"
- android:text="TestShapeButton" /></p><p></LinearLayout></p>
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:endColor="#FFFFFF" android:gradientRadius="50" android:startColor="#ff8c00" android:type="radial"/>
- <!-- 描边 -->
- <stroke android:dashGap="3dp" android:dashWidth="5dp" android:width="2dp" android:color="#dcdcdc" />
- <!-- 圆角 -->
- <corners android:radius="2dp" />
- <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
- </shape></item>
- <item android:state_focused="true"><shape>
- <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />
- <stroke android:width="2dp" android:color="#dcdcdc" />
- <corners android:radius="2dp" />
- <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
- </shape></item>
- <item><shape>
- <solid android:color="#ff9d77" />
- <stroke android:width="2dp" android:color="#fad3cf" />
- <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />
- <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
- </shape></item>
- </selector>
效果图如下:
一般状态:
按下状态:
原文地址:http://blog.csdn.net/ygc87/article/details/7673587