展会信息港展会大全

android中使用selector动态改变imageView或ImageViewButton的背景
来源:互联网   发布日期:2015-10-03 11:20:45   浏览:1826次  

导读:做应用时,可能会需要动态改变控件的背景图片,如果仅仅是简单的点击,选中之类的事件,如果靠程序中写监听的代码就显得太麻烦了,在这种情况下,你可以使用selector动态改变控件背景拉:)1。在res/drawable目录......

做应用时,可能会需要动态改变控件的背景图片,如果仅仅是简单的点击,选中之类的事件,如果靠程序中写监听的代码就显得太麻烦了,在这种情况下,你可以使用selector动态改变控件背景拉:)

1。在res/drawable目录下建一个mybutton.xml文件,根据需要,不同的状态下建立不同的item,并对应相应的图片

01 <selectorxmlns:android="http://schemas.android.com/apk/res/android">

02

03<item android:state_window_focused="false"

04android:drawable="@color/transparent"/>

05

06<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. 注意这句话-->

07

08

09<item android:state_focused="true" android:state_enabled="false"

10android:state_pressed="true"

11android:drawable="@drawable/selector_background_disabled"/>

12<item android:state_focused="true" android:state_enabled="false"

13android:drawable="@drawable/lselector_background_disabled"/>

14

15<item android:state_focused="true" android:state_pressed="true"

16android:drawable="@drawable/selector_background_transition"/>

17<item android:state_focused="false" android:state_pressed="true"

18android:drawable="@drawable/selector_background_transition"/>

19

20<item android:state_focused="true"

21android:drawable="@drawable/selector_background_focus"/>

22

23 </selector>

2。在构造layout是引用这个xml

1 <ImageButton

2android:id="@+id/ImageButton01"

3android:layout_width="wrap_content"

4android:layout_height="wrap_content"

5android:background="@drawable/mybutton">

6 </ImageButton>

这样就ok拉

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港