首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android代码兑现背景切换

2012-09-21 
android代码实现背景切换在android 中,如果要实现点击按钮,切换背景xml配置为:?xml version1.0 encodin

android代码实现背景切换

在android 中,如果要实现点击按钮,切换背景xml配置为:

?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"    android:constantSize=["true" | "false"]    android:dither=["true" | "false"]    android:variablePadding=["true" | "false"] >    <item        android:drawable="@[package:]drawable/drawable_resource"        android:state_pressed=["true" | "false"]        android:state_focused=["true" | "false"]        android:state_selected=["true" | "false"]        android:state_checkable=["true" | "false"]        android:state_checked=["true" | "false"]        android:state_enabled=["true" | "false"]        android:state_window_focused=["true" | "false"] /></selector>

?这段xml转换成java代码为:

StateListDrawable drawable=new StateListDrawable();drawable.addState(new int []{android.R.attr.state_pressed}, Drawable.createFromStream(getResources().getAssets().open("mlist1_focush.png"), "src"));drawable.addState(new int []{}, Drawable.createFromStream(getResources().getAssets().open("mlist1_unfocush.png"), "src"));

?然后? imageView.setBackgroundDrawable(drawable);

imageView.setBackgroundDrawable(drawable);

?这样就实现了同样的功能,可以用这listview等控件上,非常有用

?

热点排行