在Android中使按钮半透明

4

可能是重复问题:
如何在Android中为视图设置不透明度(Alpha)

我想问两个问题:

1) 我想让我的main.xml中的按钮,也就是第一个屏幕上的按钮,半透明。应该是这样的,背景图片可以透过它看到一部分。但是按钮应该保持其正常大小和外观。

2) (这是关于另一个视图中的另一个按钮)我使用以下方法改变了按钮的背景:

android:background="#2563EA"

现在当它被点击时,它不会改变颜色。我该怎么重置它。还有谁能告诉我如何给它一个新的 onClick 颜色。


这里已经有一个关于在Android中设置按钮透明度的线程。https://dev59.com/t3E85IYBdhLWcg3wUBuz - Turnsole
如果你想问两个问题,就请问两个问题,不要把它们放在一个问题里 =) - Rob
2个回答

3

要根据状态改变按钮的外观,请使用StateList


2
  1. Use the alpha property for the transparency of the color. Also see this thread.
  2. You can dynamically change the color by using the OnTouch event. Or better, you can assign the background in the XML to be a selector.

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
              android:drawable="@drawable/button_pressed" /> <!-- pressed -->
        <item android:state_focused="true"
              android:drawable="@drawable/button_focused" /> <!-- focused -->
        <item android:drawable="@drawable/button_normal" /> <!-- default -->
    </selector>
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接