东子哥7 2013-04-12 11:49 采纳率: 0%
浏览 4263
已采纳

关于设置Android Button按钮状态(normal,focused,pressed)图片,有没有简便的方法

Android Button按钮状态(normal,focused,pressed)的图片
大都用drawable "selector" 来实现
类似
btn_background.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/btn_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/btn_normal" /> <!-- focused -->
     <item android:drawable="@drawable/btn_normal" /> <!-- default -->
</selector>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/btn_background"
    />
</LinearLayout>

上面这种做法,意味每一个按钮都要定义它的selector,如果按钮很多,这工作量不说,也太繁琐了,现在就碰到一个项目,里面需要单独定义的按钮非常多。
不知各位大侠,有没有想出方便的方案。多谢了

  • 写回答

2条回答 默认 最新

  • huzgd 2013-04-14 08:06
    关注

    可以考虑继承Button或ImageButton做个自定义控件,并在自定义控件中加两个自定义状态图片的属性(如android:focusedBackground,android:pressedBackground),然后就可以直接在界面的XML里写这两个属性,如:

        < com.xxx.xxx.MyImageButton android:id="xxx"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="xxx"
      android:focusedBackground="xxx"
      android:pressedBackground="xxx"
    />
    

    关于自定义控件中如何读取自定义属性,网上有现成的例子资料参考,如:
    http://www.cnblogs.com/xiaoQLu/archive/2011/07/20/2112004.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog