我有一个按钮如下:

<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),但它给了我一个错误。


当前回答

如果你使用Kotlin,像这样设置alpha很容易

imageView.alpha= 0.5F

其中值必须为浮点数。

其他回答

我猜你可能已经找到了答案,但如果没有(对于其他开发人员),你可以这样做:

btnMybutton.getBackground().setAlpha(45);

这里我将不透明度设置为45。你可以从0(完全透明)到255(完全不透明)之间任意设置它

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

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

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

我在ICS/JB中遇到过这个问题,因为Holo主题的默认按钮由略透明的图像组成。作为背景,这一点尤其明显。

姜饼vs. ICS+:

复制每个分辨率下的所有可绘制状态和图像,并使透明图像实心是一种痛苦,所以我选择了一个更脏的解决方案:将按钮包裹在有白色背景的支架中。这是一个粗糙的XML绘图(ButtonHolder),它正是这样做的:

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              style="@style/Content">
  <RelativeLayout style="@style/ButtonHolder">
      <Button android:id="@+id/myButton"
              style="@style/Button"
              android:text="@string/proceed"/>
    </RelativeLayout>
</LinearLayout>

ButtonHolder.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@color/white"/>
    </shape>
  </item>

</layer-list>

styles.xml

.
.
.      
  <style name="ButtonHolder">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:background">@drawable/buttonholder</item>
  </style>

  <style name="Button" parent="@android:style/Widget.Button">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:textStyle">bold</item>
  </style>
.
.
.

然而,这将导致白色边框,因为Holo按钮图像包括空白,以说明按下的空间:

所以解决方案是给白色背景一个边距(对我来说是4dp)和圆角(2dp),以完全隐藏白色,但使按钮坚固:

ButtonHolder.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <shape android:shape="rectangle">
      <solid android:color="@android:color/transparent"/>
    </shape>
  </item>

  <item android:top="4dp" android:bottom="4dp" android:left="4dp" android:right="4dp">
    <shape android:shape="rectangle">
      <solid android:color="@color/white"/>
      <corners android:radius="2dp" />
    </shape>
  </item>

</layer-list>

最终结果是这样的:

你应该针对v14+使用这种风格,而针对Gingerbread/Honeycomb调整或排除它,因为它们的原生按钮图像大小与ICS和JB的不同(例如,姜饼按钮后面的这种风格导致按钮下面有一小块白色)。

对于视图,您可以通过以下方法设置不透明度。

view_name.setAlpha(float_value);

属性view.setAlpha(int)对于大于11的API版本已弃用。从今以后,使用. setalpha (0.5f)这样的属性。

尽管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.