透明背景

11

如何让背景透明度为50%?
假设要让AbsoluteLayout的背景颜色为深色,但仍能看到其后面的内容。


AbsoluteLayout已被弃用,您可能需要使用FrameLayout代替。 - Cheryl Simon
我不能使用其他任何东西,我需要能够在运行时向屏幕添加项目,并设置它们的x、y、宽度和高度。使用帧布局可以实现这个吗? - aryaxt
1个回答

31

您可以为所需的活动应用透明主题。在 /res/values/style.xml 中创建一个新样式。

<resources>
<style name="Transparent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
<item name ="android:windowBackground">@color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
</resources>

transparent的值为

<color name="transparent">#00000000</color>

现在在AndroidManifest.xml文件中声明该活动的主题为您刚刚创建的主题。

<activity android:name="MyActivity" android:theme="@style/Transparent"></activity>

同样的问题,但是在标准的Android按钮上(默认的灰色按钮),你能让它变成50%透明吗?我只是想知道... - Hubert
回答这个问题:如果你只想要50%的透明度,你需要将前两个数字设为50而不是00。因此,颜色定义中的前两个数字表示透明度水平,所以应该设置#50000000。 - Booger
4
@Booger,你关于#50导致50%透明度的说法是不正确的。颜色(和透明度)的范围从十六进制表示的00到FF,或者从十进制的0到255。因此,50%透明度的正确HTML值应为#7F000000。 - Saran
这个方案可行。我的应用程序是为了禁用触摸,但仍允许远程按下应用程序的底层按钮,因此我不能禁用按钮本身。这是一个非常聪明的解决方案。<Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="match_parent" android:text="" android:background="#00000000" android:clickable="true"/> - Allen Edwards

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