Android自定义shape的使用示例
程序员文章站
2023-11-25 10:29:58
mainactivity如下: 复制代码 代码如下: package cn.testshape; import android.os.bundle; import andr...
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>
复制代码 代码如下:
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>
上一篇: 3dmax怎么制作一个@样式的沙发模型?
下一篇: cad怎么在等轴测面中绘制直线与弧线?