简单记录——android selector对应的java实现:StateListDrawable
2013-12-11 17:01
429 查看
xml代码:
color.xml:<?xml version="1.0" encoding="utf-8"?> <resources> <color name="btn_normal">#88FE9D3D</color> <color name="btn_pressed">#EEFE9D3D</color> </resources>selector_btn.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/btn_pressed" android:state_pressed="true"/> <item android:drawable="@color/btn_normal" android:state_pressed="false"/> </selector>java中调用:
TextView tvXML = (TextView) findViewById(R.id.tvXML); tvXML.setClickable(true); tvXML.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_btn));
java代码:
/** 获取一个selector */ public Drawable getSelectorDrawable() { ColorDrawable cdNormal = new ColorDrawable(Color.parseColor("#88FE9D3D")); ColorDrawable cdPressed = new ColorDrawable(Color.parseColor("#EEFE9D3D")); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] { -android.R.attr.state_pressed }, cdNormal); sld.addState(new int[] { android.R.attr.state_pressed }, cdPressed); return sld; }
java中调用:
TextView tvJ***A = (TextView) findViewById(R.id.tvJ***A); tvJ***A.setClickable(true); tvJ***A.setBackgroundDrawable(getSelectorDrawable());
StateListDrawable:http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
State List:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
相关文章推荐
- Android-在XML和Java代码中设置背景在不同状态的效果: <selector>/StateListDrawable
- java代码中实现android背景选择的selector-StateListDrawable的应用
- [Android]代码实现ColorStateList及StateListDrawable
- 【转】java代码中实现android背景选择的selector-StateListDrawable的应用
- android 代码编写selector--StateListDrawable使用
- [Android]代码实现ColorStateList及StateListDrawable
- java代码中实现android背景选择的selector-StateListDrawable的应用
- [Android]代码实现ColorStateList及StateListDrawable
- java代码中实现android背景选择的selector-StateListDrawable的应用
- [Android]代码实现ColorStateList及StateListDrawable
- java代码中实现android背景选择的selector-StateListDrawable的应用
- android中ColorStateList及StateListDrawable设置Selector
- [Android]代码实现StateListDrawable
- java代码中实现android背景选择的selector-StateListDrawable的应用
- [Android]代码实现ColorStateList及StateListDrawable
- 天天记录 - Android refreshDrawableState源码及流程简单分析
- [Android]代码实现ColorStateList及StateListDrawable
- Android -- selector&&StateListDrawable
- android StateListDrawable
- 如何在Android源码里查找Java中native方法对应的C++实现