更改视图的 opacity/transparency 并影响 child 视图

Change the opacity/transparency of a view and affect the child views too

我有一个相对布局 child TextViews.
parent RelativeLayout 的背景是白色的,我想知道如何更改 alpha 以编程方式更改整个视图的不透明度(包括 children)。
我正在尝试:

getBackground().setAlpha(0.4);  

但这需要一个整数而不是一个浮点数。 如果我这样做:

getBackground().setAlpha((int)(0.4 * 255));  

后者改变了视图,但使它比我想要的更暗。另外 children 似乎没有变化。它似乎只影响背景,而我想要一些让一切都变得更好的东西 "grayed" out/less transparent.
我做错了什么?

您是否尝试过在布局文件中使用 android:background="#88000000"。您可以根据需要更改 alpha 和颜色值。

那是因为您正在更改布局的背景,而不是布局本身。因此,不要使用 myRelativeLayout.getBackground().setAlpha(),而是使用:myRelativeLayout.setAlpha(0.4f).