如何在Android中设置不透明度(Alpha)以供查看

我有一个button,如下所示:

<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); 

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

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

我刚刚发现你的问题,而与TextView类似的问题。 我能够解决它,通过扩展TextView和重写onSetAlpha 。 也许你可以尝试类似的button:

 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; } } 

我很惊讶别人的更复杂的答案。

XML

您可以非常简单地在xml中的button(或任何其他视图)的颜色定义中定义alpha:

 android:color="#66FF0000" // Partially transparent red 

在上面的例子中,颜色是部分透明的红色。

在定义视图的颜色时,格式可以是#RRGGBB#AARRGGBB ,其中AA是hex#AARRGGBB值。 FF将是完全不透明的, 00将是完全透明的。

dynamic

如果您需要dynamic更改代码中的不透明度,请使用

 myButton.getBackground().setAlpha(128); // 50% transparent 

INT的范围从0 (完全透明)到255 (完全不透明)。

我想你可能已经find了答案,但是如果没有(和其他开发者),你可以这样做:

 btnMybutton.getBackground().setAlpha(45); 

在这里我已经设置不透明度为45.你可以基本上从0 (完全透明)到255 (完全不透明)

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

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

然后将您的button背景设置为该颜色:

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

如果你想玩button的形状,你可以做的另一件事是创build一个形状可绘制的资源 ,在那里你设置的属性button应该看起来像:

文件:res / drawable / rounded_corner_box.xml

 <?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> 

然后用它作为button背景:

  android:background="@drawable/rounded_corner_box" 

根据android文档视图alpha是一个0和1之间的值。所以设置它使用这样的东西:

 View v; v.setAlpha(.5f); 

从上面更容易。 默认的alpha属性是button

 android:alpha="0.5" 

完全透明的范围是0,完全不透明的范围是1。

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

以上是我知道的东西…我认为创build一个自定义button类是最好的主意

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

虽然btnMybutton.getBackground().setAlpha(45); 是不错的主意,它只是将alpha应用于背景而不是整个视图。

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

Doc说:

设置视图的不透明度。 这是从0到1的值,其中0表示视图完全透明,1表示视图完全不透明。 如果此视图重写SetAlpha(int)以返回true,则此视图负责应用不透明度本身。 否则,调用此方法相当于调用setLayerType(int,android.graphics.Paint)并设置硬件层。 请注意,将alpha设置为半透明值(0 <alpha <1)可能会影响性能。 通常最好less用短暂地使用alpha属性,就像在淡入淡出的animation中一样。

我遇到了ICS / JB的这个问题,因为Holo主题的默认button由稍微透明的图像组成。 对于背景,这是特别明显的。

姜饼与ICS +:

姜饼ICS

复制每个分辨率的所有可绘制的状态和图像,并使透明图像变为固定是一种痛苦,所以我select了一个更脏的解决scheme:将button包裹在具有白色背景的支架中。 这是一个粗略的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> . . . 

但是,这会导致白色边框,因为Holobutton图像包含边距来说明按下的空间:

太白了太多的白色按下

所以解决的办法是给白色背景边缘(4dp为我工作)和圆angular(2dp)完全隐藏白色,但使button固体:

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调整或排除它,因为它们的原始button图像大小与ICS和JB不同(例如姜饼button背后的确切样式导致button下面有一小块白色)。

对于textView颜色的API <11,我做了以下操作:

 int textViewColor = textView.getTextColors().getDefaultColor(); textView.setTextColor(Color.argb(128, Color.red(textViewColor), Color.green(textViewColor), Color.blue(textViewColor))); //50% transparent 

有点麻烦,但嘿,它的工作原理:-)

对于一个视图,你可以通过以下设置不透明度。

 view_name.setAlpha(float_value); 

对于API版本大于11,不推荐使用view.setAlpha(int)属性。因此,使用类似.setAlpha(0.5f)属性。

我知道这已经有一堆的答案,但我发现,button只是最简单的创build自己的.xmlselect器,并将其设置为所述button的背景。 这样,你也可以改变它的状态,当按下或启用等。 这是我使用的一个快速片段。 如果要为任何颜色添加透明度,请添加前导hex值(#XXcccccc)。 (XX ==“alpha的颜色”)

 <?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>