【问题标题】:Changing background or foreground button color dynamically in android在android中动态更改背景或前景按钮颜色
【发布时间】:2012-08-03 22:18:32
【问题描述】:

假设我的应用中有一个如下所示的页脚,定义在诸如 footer.xml 之类的 XML 文件中:

 <LinearLayout android:id="@+id/llfooter"
        android:layout_weight="1" android:layout_width="fill_parent"
        android:orientation="horizontal" android:layout_height="0dp"
        android:visibility="visible" android:background="@drawable/fbg"
        android:weightSum="5.0" android:gravity="center"
        android:layout_margin="0dp">

        <Button android:id="@+id/home" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/home" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/issue" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@android:drawable/ic_menu_send" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/browse" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@android:drawable/ic_menu_slideshow" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/search" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/search" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:layout_height="wrap_content" android:id="@+id/favorite"
            android:background="@drawable/favorite" android:layout_width="wrap_content"
            android:focusable="true"
            android:clickable="true"
            android:layout_weight="1"
            android:textColor="#FFFFFF" android:padding="10px"></Button>
    </LinearLayout>

现在,问题是 homeissuebrowse 等是 PNG 图标,当我点击它们时,用户不能有触摸的反馈,因为它们保持不变。

我想在按下它们时更改背景颜色(例如,稍微亮一点)。我知道我可以为每个按钮写下一个 XML drawables(),如下所示

  <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:state_focused="false" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:drawable="@drawable/bgnorm" /> 
  </selector>

.. 但是如果我有 10 个按钮(例如,5 个用于页脚,5 个用于页眉),我应该创建其他 10 个更改背景的按钮(因此更多地使用图形编辑器和 .apk 更重,因为更多的光栅图标..) . 有没有办法创建(甚至在 java 中)更浅的颜色“onClick”和普通颜色“onRelease”,资源中的每个功能只有一个图标?

有什么建议吗?

Tnx 提前。 加博

【问题讨论】:

    标签: android xml button colors onclick


    【解决方案1】:

    您可以在代码中的 onTouch 中获取按钮位图并更改颜色,但这是个坏主意。 选择器是最好的解决方案。

    【讨论】:

      【解决方案2】:

      使用 ImageButton,并将 android:src 参数设置为具有透明背景的可绘制按钮,然后将 android:background 值设置为例如在选择时会改变颜色的选择器可绘制对象。

      这样,您的图标就有一组可绘制对象,而一个仅用于根据按钮状态变化的背景的可绘制对象

      【讨论】:

      • 非常感谢您的正确答案,Jucas。它就像我现在想要的那样工作!
      • @gabolander 如果此答案解决了您的问题,请选择此答案作为正确答案
      猜你喜欢
      • 2013-09-03
      • 2014-12-26
      • 2021-07-05
      • 2021-03-18
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多