在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等控件上,非常有用
本文介绍如何使用XML配置文件和Java代码实现Android应用中按钮的状态切换效果,包括按下、聚焦等不同状态下的背景变化。
614

被折叠的 条评论
为什么被折叠?



