1、首先摆上一个button自定义的例子:只含有shape标签的使用
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#4169E1" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="20dp"
android:top="5dp"
android:right="20dp"
android:bottom="5dp"
/>
</shape>
这个新建一个xml文件(search_btn_shape)放在drawable文件夹中,怎么引用呢?
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:background="@drawable/search_btn_shape"
android:text="@string/btnSearch" />
2、至于 selector,常用来设置组件有焦点,无焦点,点击时所表现的样式,这里也给出一个例子:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 未获得焦点-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false"
android:drawable="@drawable/grid_focus" />
<!-- 获得焦点-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false"
android:drawable="@drawable/grid_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false"
android:drawable="@drawable/grid_focus" />
<!-- 点击-->
<item android:state_pressed="true" android:drawable="@drawable/grid_focus" />
</selector>
3、怎么讲selector和shape组合使用呢?
方法一:其实在例2种的android:drawable="?",这里的?完全可以给shape.xml来代替,这样就完成了组合使用。
方法二:直接在item标签中包含shape标签也行。举个例子:(出处)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.android.com/apk/res/android">
<item Android:state_selected="true">
<shape>
<gradient Android:angle="270" android:endColor="#99BD4C"
android:startColor="#A5D245" />
<size Android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
<item Android:state_pressed="true">
<shape>
<gradient Android:angle="270" android:endColor="#99BD4C"
android:startColor="#A5D245"/>
<size Android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
<item>
<shape>
<gradient Android:angle="270" android:endColor="#A8C3B0"
android:startColor="#C6CFCE" />
<size Android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
4、那么shape标签都有哪些属性呢?(这部分摘自网上:出处)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 圆角 -->
<corners
android:radius="9dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/><!-- 设置圆角半径 -->
<!-- 渐变 -->
<gradient
android:startColor="@android:color/white"
android:centerColor="@android:color/black"
android:endColor="@android:color/black"
android:useLevel="true"
android:angle="45"
android:type="radial"
android:centerX="0"
android:centerY="0"
android:gradientRadius="90"/>
<!-- 间隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的间隔 -->
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 宽度和高度 -->
<!-- 填充 -->
<solid
android:color="@android:color/white"/><!-- 填充的颜色 -->
<!-- 描边 -->
<stroke
android:width="2dp"
android:color="@android:color/black"
android:dashWidth="1dp"
android:dashGap="2dp"/>
</shape>
PS:如有侵权请联系我删掉