我有一个按钮如下:

<Button 
     android:text="Submit" 
     android:id="@+id/Button01" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">
</Button>

在onCreate()事件中,我像这样调用Button01:

setContentView(R.layout.main);

View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);

在应用程序中有一个背景,我想在这个提交按钮上设置一个不透明度。如何为这个视图设置不透明度?是可以在java端设置,还是可以在main.xml文件中设置?

在java方面,我尝试了Button01.mutate(). setalpha(100),但它给了我一个错误。


当前回答

尽管btnMybutton.getBackground () .setAlpha (45);这是个好主意,它只是对背景应用alpha,而不是整个视图。

如果你想应用alpha视图使用btnMybutton.setAlpha(0.30f);代替。这应用不透明度的视图。它接受0到1之间的值。

医生说:

Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque. If this view overrides onSetAlpha(int) to return true, then this view is responsible for applying the opacity itself. Otherwise, calling this method is equivalent to calling setLayerType(int, android.graphics.Paint) and setting a hardware layer. Note that setting alpha to a translucent value (0 < alpha < 1) may have performance implications. It is generally best to use the alpha property sparingly and transiently, as in the case of fading animations.

其他回答

我建议你做的是在colors.xml文件中创建一个自定义ARGB颜色,如:

<resources>
<color name="translucent_black">#80000000</color>
</resources>

然后将按钮背景设置为该颜色:

android:background="@android:color/translucent_black"

如果你想要处理按钮的形状,你可以做的另一件事是创建一个shape drawable资源,在那里你可以设置按钮应该是什么样子:

xml: res / drawable - rounded_corner_box文件。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#80000000"
        android:endColor="#80FFFFFF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

然后使用它作为按钮背景:

    android:background="@drawable/rounded_corner_box"

我刚刚发现你的问题,而有类似的问题与TextView。我能够解决它,通过扩展TextView和覆盖onSetAlpha。也许你可以在你的按钮上尝试类似的东西:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class AlphaTextView extends TextView {

  public AlphaTextView(Context context) {
    super(context);
  }

  public AlphaTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  public boolean onSetAlpha(int alpha) {
    setTextColor(getTextColors().withAlpha(alpha));
    setHintTextColor(getHintTextColors().withAlpha(alpha));
    setLinkTextColor(getLinkTextColors().withAlpha(alpha));
    return true;
  }
}

尽管btnMybutton.getBackground () .setAlpha (45);这是个好主意,它只是对背景应用alpha,而不是整个视图。

如果你想应用alpha视图使用btnMybutton.setAlpha(0.30f);代替。这应用不透明度的视图。它接受0到1之间的值。

医生说:

Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque. If this view overrides onSetAlpha(int) to return true, then this view is responsible for applying the opacity itself. Otherwise, calling this method is equivalent to calling setLayerType(int, android.graphics.Paint) and setting a hardware layer. Note that setting alpha to a translucent value (0 < alpha < 1) may have performance implications. It is generally best to use the alpha property sparingly and transiently, as in the case of fading animations.

我知道这已经有一堆答案,但我发现,对于按钮,它只是最简单的创建自己的.xml选择器,并将其设置为该按钮的背景。这样你也可以在按下或启用时改变它的状态。下面是我使用的一个简短的片段。如果你想为任何颜色添加透明度,添加一个前导十六进制值(#XXcccccc)。(XX == "alpha of color")

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
android:background="@android:color/transparent"

以上是我所知道的… 我认为创建一个自定义按钮类是最好的主意

API等级11 最近我遇到了这个android:alpha xml属性,它的值在0到1之间。相应的方法是setAlpha(float)。