Android布局背景alpha

use*_*447 20 android background

嗨我有一个布局,我用来填充我的页面,我必须在我drawable folder的布局中设置一个背景图像.

我想alpha value将图像设置为相当低的几乎使图像像水印一样.

我的xml看起来像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/main_background" >
Run Code Online (Sandbox Code Playgroud)

如您所见,我已为布局分配了一个ID

我想在我的创造中我可以做这样的事情吗?

View backgroundimage = (View) findViewById(R.id.background);
backgroundimage.setAlpha(80);
Run Code Online (Sandbox Code Playgroud)

这不起作用,但我怀疑它是因为我试图将背景作为View我应该把它投射为什么?

Tan*_* Ke 42

尝试使用:

Drawable.setAlpha();
Run Code Online (Sandbox Code Playgroud)

你应该做这样的事情:

View backgroundImage = findViewById(R.id.background);
Drawable background = backgroundImage.getBackground();
background.setAlpha(80);
Run Code Online (Sandbox Code Playgroud)

  • 100%不透明度等于255,而不是1000 (4认同)
  • 尽管此方法有效,但似乎要获得原始状态或100%alpha,您可能需要使用0-1000比例.因此,如果你想让一个按钮看起来50%alpha,你实际上可能需要setAlpha(200)而不是setAlpha(50)......至少在按钮上使用API​​ 19 ...所以将按钮重置为这是原始的样子,我不得不设置Alpha(1000)...... (2认同)

Kau*_*hik 35

如果你想在xml中设置alpha,那么你可以试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#CC000000" >
Run Code Online (Sandbox Code Playgroud)

前2位用于设置alpha这里alpha是80%接下来的6位数字代码

因此,alpha以十六进制编码设置.FF = 100%且00 = 0%,因此80%不是十六进制的80,对于更多标准值,请参阅文章.