本文实例讲述了Android编程实现改变控件背景及形态的方法。分享给大家供大家参考,具体如下:
1. 改变背景
在res/drawable下创建一个xml文件:
1
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
// 控件被按下时候的背景
< item android:state_pressed = "true" android:drawable = "@drawable/search_bar_edit_pressed" />
// 控件被选择时候的背景
< item android:state_selected = "true" android:drawable = "@drawable/search_bar_edit_pressed" />
// 控件一般状态下的背景
< item android:drawable = "@drawable/search_bar_edit_normal" />
</ selector >
|
2. 改变形态
1
2
3
4
5
6
7
8
9
10
11
|
< shape >
// 渐变
< gradient android:startColor = "#8600ff" />
// 描边
< stroke android:width = "2dp" android:color = "#000000" />
// 圆角
< corners android:radius = "5dp" />
// 边距
< padding android:left = "10dp" android:top = "10dp"
android:bottom = "10dp" android:right = "10dp" />
</ shape >
|
在布局文件上引用:
1
|
android:background= "@drawable/"
|
希望本文所述对大家Android程序设计有所帮助。