您的位置:首页 > 移动开发 > Android开发

Android自定义Button字体颜色

2012-07-14 22:03 405 查看
我们可以使用selector来实现Button的特效,如图所示:



默认情况



获得焦点的时候



点击按钮

main.xml

Xml代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="按下或者获得焦点Button会变不同颜色"

<SPAN style="COLOR: #ff0000">android:textColor="@color/button_text" </SPAN>/>

</LinearLayout>

www.2cto.com

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="按下或者获得焦点Button会变不同颜色"

android:textColor="@color/button_text" />

</LinearLayout>

XML 文件保存在res/color/button_text.xml

Xml代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed -->

<item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused -->

<item android:color="#ff000000"/> <!-- default -->

</selector>


背景选择器-selector


概述


在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。


selector中的常用属性


android:state_selected 控件选中状态,可以为true或false

android:state_focused 控件获得焦点状态,可以为true或false

android:state_pressed 控件点击状态,可以为true或false

android:state_enabled 控件使能状态,可以为true或false

android:state_checkable 控件可勾选状态,可以为true或false

android:state_checked 控件勾选状态,可以为true或false

注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。

android:window_focused 应用程序窗口焦点状态,可以为true或false

android:color 定义特定状态的颜色

为16进制颜色。这个颜色由rgb值指定,可带alpha,必须以”#“开头,后面跟随alpha-red-green-blue信息,格式可以为:

#rgb

#argb

#rrggbb

#aarrggbb


使用selector设置背景


把下面的XML保存成.xml文件(比如list_item_bg.xml),运行时系统会根据ListView中列表项的状态来使用相应的背景图片。

drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片 -->
<item android:drawable="@drawable/pic1" />

<!-- 没有焦点时的背景图片 -->
<item android:state_window_focused="false"
android:drawable="@drawable/pic1" />

<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable= "@drawable/pic2" />

<!-- 触摸模式下单击时的背景图片 -->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pic3" />

<!--选中时的图片背景  -->
<item android:state_selected="true"
android:drawable="@drawable/pic4" />

<!--获得焦点时的图片背景  -->
<item android:state_focused="true"
android:drawable="@drawable/pic5" />
</selector>


[title3]
使用方法[/title3]

第一种是在listview中配置android:listSelector=”@drawable/list_item_bg”

第二种是在listview的item中添加属性android:background=”@drawable/list_item_bg”

第三种是java代码中使用:
Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
listview.setSelector(drawable);


注:列表有时候为黑的情况,需要加上下面的代码使其透明:
android:cacheColorHint="@android:color/transparent"



使用selector设置字体颜色


drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FF0000" />
<item android:state_focused="true" android:color="#00FF00" />
<item android:state_pressed="true" android:color="#0000FF" />
<item android:color="#000000" />
</selector>


[title3]
使用方法[/title3]

android:textColor="@drawable/button_color"



更复杂的效果


还可以实现更复杂的效果,例如渐变等等。 drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。-->
<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>
</item>
<item android:state_focused="true">
<!-- 定义当button获得 focus时的形态 -->
<shape>
<gradient android:startColor="#eac100"/>
<stroke android:width="2dp"
android:color="#333333"
color="#ffffff"/>
<corners android:radius="8dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
</shape>
</item>
</selector>


[title3]
使用方法[/title3]

android:background="@drawable/button_color"
android:focusable="true"


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: