MainActivity如下:
复制代码 代码如下:
package cn.testshape;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 自定义shape的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml如下:
复制代码 代码如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="250dip"
android:layout_height="50dip"
android:text="测试自定义shape的使用"
android:background="@drawable/background_selector"
android:textColor="@drawable/textcolor_selector"
android:layout_centerInParent="true"
android:gravity="center"
/>
</RelativeLayout>
background_selector.xml如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_shape" android:state_pressed="true"/>
<item android:drawable="@drawable/default_shape"/>
</selector>
default_shape.xml如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定义边框颜色 -->
<solid android:color="#d1d1d1" />
<!-- 定义圆角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>
pressed_shape.xml如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定义边框颜色 -->
<solid android:color="#7bb3f8" />
<!-- 定义圆角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>
textcolor_selector.xml如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#ffffff" android:state_pressed="true"/>
<item android:color="#000000"/>
</selector>