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

不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述如何设置背景图片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>

【如何设置背景图片Android xml文件的透明度】
答案您可以设置alpha属性:
android:alpha="0.4"

通过代码:
yourView.setAlpha(0.5f);

只有背景:
yourView.getBackground().setAlpha(120); // here the value is an integer not float

另一答案它可以使用自定义drawable完成。它是一个非常古老的帖子,但我认为我应该回答,可能对某人有帮助。
  1. 使用位图创建一个可绘制的xml资源,根据bg.xml说。
  2. 将android:src =https://www.songbingjia.com/android/“@ drawable / background”设置为您想要作为背景的图像。
  3. 根据所需的不透明度将android:alpha =“0.4”设置为0到1之间的任何值。
  4. 现在将根布局的背景设置为新创建的可绘制xml资源,即bg.xml。这将仅更改布局背景图像的不透明度,而不会影响布局的子元素的不透明度。
Resurtses: bg.hml
< ?xml version="1.0" encoding="utf-8"?> < bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="https://www.songbingjia.com/android/@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"

仅淡化背景图像而不影响子视图。
另一答案您可以创建图层列表资源并将其设置为视图的背景属性(布局)
background_image_with_alpha.xml
< ?xml version="1.0" encoding="utf-8"?> < layer-list xmlns:android="http://schemas.android.com/apk/res/android" > < item> < bitmap android:alpha="0.2" android:src="https://www.songbingjia.com/android/@drawable/xxxxxx" android:gravity="fill"> < /bitmap> < /item> < /layer-list >

另一答案你可以使用alpha属性(http://developer.android.com/reference/android/view/View.html#attr_android:alpha)为你的View
android:alpha="0.4"

另一答案尝试使用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之间)

    推荐阅读