如何设置Android xml文件的背景图片的透明度

我已经把一个图像作为我的Android应用程序的背景下面的一行代码:

android:background="@drawable/background" 

我现在想让它透明40%,但在XML文件中如何可能? 我的exm文件如下所示:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:alpha="0.6" android:orientation="vertical" android:padding="30dp" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="5dp" android:text="Welcome" android:textColor="#292421" android:textSize="17sp" android:textStyle="bold" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time." android:textColor="#292421" android:textSize="17sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="tracking" android:text="@string/tracking_service" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="20dp" > </LinearLayout> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="help" android:text="Help" /> </LinearLayout> 

你可以设置alpha属性:

 android:alpha="0.4" 

通过代码:

 yourView.setAlpha(0.5f); 

只有背景:

 yourView.getBackground().setAlpha(120); // here the value is an integer not float 

它可以使用自定义drawable来完成。 这是一个很老的post,但认为我应该回答,可能对某人有帮助。

  1. 位图创build一个可绘制的xml资源作为bg.xml
  2. android:src =“@ drawable / background”设置为你想要的图像作为背景。
  3. 根据需要的不透明度将android:alpha =“0.4”设置为0到1之间的任何值。
  4. 现在将根布局的背景设置为新创build的可绘制xml资源,即bg.xml 。 这只会改变布局的背景图像的不透明度,而不影响布局的子元素的不透明度。

资源:
bg.xml

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/background" android:alpha="0.4"/> 

这是你的布局现在的样子:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:orientation="vertical" android:padding="30dp" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="5dp" android:text="Welcome" android:textColor="#292421" android:textSize="17sp" android:textStyle="bold" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time." android:textColor="#292421" android:textSize="17sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="tracking" android:text="@string/tracking_service" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="20dp" > </LinearLayout> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="help" android:text="Help" /> </LinearLayout> 

如果你已经设置了一个图像作为背景然后

  android:alpha="0.x" 

会导致整个屏幕(子视图)褪色。 相反,你可以利用

 android:backgroundTint="#80FFFFFF" android:backgroundTintMode="src_over" 

只淡化背景图像而不影响子视图。

尝试使用Drawable.setAlpha();

 View backgroundImage = findViewById(R.id.background_image); Drawable background = backgroundImage.getBackground(); background.setAlpha(80); 

在你的xml文件中设置

 android:alpha="0.4" 

这将从0到255变化。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:alpha="0.6" android:orientation="vertical" android:padding="30dp" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="5dp" android:text="Welcome" android:textColor="#292421" android:textSize="17sp" android:textStyle="bold" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time." android:textColor="#292421" android:textSize="17sp" /> <LinearLayout android:background="@drawable/background" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="tracking" android:text="@string/tracking_service" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="20dp" > </LinearLayout> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="85dp" android:layout_weight="1" android:background="#E6E6FA" android:onClick="help" android:text="Help" /> </LinearLayout> 

你可以使用alpha属性android:alpha =“0.5”; (在0和1之间)

您可以使用View alpha属性( http://developer.android.com/reference/android/view/View.html#attr_android:alpha ):

 android:alpha="0.4"