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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22406961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:56:11  来源:igfitidea点击:

How to set transparency of a background image Android xml file

androidxmlandroid-layout

提问by user3393962

I have put an image in as the background of my android application with the following line of code:

我已经使用以下代码行将图像作为我的 android 应用程序的背景:

 android:background="@drawable/background"

I now want to make it transparent by 40% but how is this possible within the xml file? My exm file is shown below:

我现在想让它透明 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>

回答by Decoy

You can set the alpha property:

您可以设置 alpha 属性:

android:alpha="0.4"

via code :

通过代码:

yourView.setAlpha(0.5f);

only the background :

只有背景:

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

回答by Pramod Garg

If you have set an image as background then

如果您已将图像设置为背景,则

 android:alpha="0.x"

will cause entire screen (child views) to fade. Instead you can make use of

将导致整个屏幕(子视图)褪色。相反,您可以使用

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

to fade only the background image without affecting the child views.

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

回答by Monish Kamble

It can be done using custom drawable. Its a very old post but thought I should answer, might be helpful for someone.

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

  1. Create a drawable xml resource with bitmapas the root say bg.xml.
  2. set android:src="@drawable/background"to the image which you want as the background.
  3. set android:alpha="0.4"to any value between 0 to 1 as per the required opacity.
  4. now set the background of the root layout as newly created drawable xml resource i.e., bg.xml. This will only change the opacity of the background image of the layout without effecting opacity of the child elements of the layout.
  1. 创建一个以位图为根的可绘制 xml 资源,比如bg.xml
  2. android:src="@drawable/background"设置为您想要作为背景的图像。
  3. 根据所需的不透明度将android:alpha="0.4" 设置为 0 到 1 之间的任何值。
  4. 现在将根布局的背景设置为新创建的可绘制 xml 资源,即bg.xml。这只会更改布局背景图像的不透明度,而不会影响布局子元素的不透明度。

Resources :
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"/>

This is how your layout will look now :

这是您的布局现在的外观:

<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>

回答by Pep Vivó

you can create a layer-list resource and set it to background property of your view (layout)

您可以创建一个图层列表资源并将其设置为您的视图(布局)的背景属性

background_image_with_alpha.xml

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="@drawable/xxxxxx"
            android:gravity="fill">

        </bitmap>
    </item>

</layer-list >

回答by Ranjeet Luhar

You can use alpha property android:alpha="0.5"; (Between 0 and 1 )

您可以使用 alpha 属性 android:alpha="0.5"; (介于 0 和 1 之间)

回答by nikis

You can use alphaproperty (http://developer.android.com/reference/android/view/View.html#attr_android:alpha) for your View:

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

android:alpha="0.4"

回答by nikis

Try to use Drawable.setAlpha();

尝试使用 Drawable.setAlpha();

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

回答by Piyush

set in your xml file

在你的 xml 文件中设置

android:alpha="0.4"

which will be varies from 0 to 255.

这将从 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>

回答by Atul Giri

just use this line in your .xml file android:background="#ddxxxxxx"replace the xxxxxx with any color you want. refer https://alvinalexander.com/android/how-background-image-behind-translucent-opaque-textviewfor more details.

只需在您的 .xml 文件中使用这一行, android:background="#ddxxxxxx"用您想要的任何颜色替换 xxxxxx。有关更多详细信息,请参阅https://alvinalexander.com/android/how-background-image-behind-translucent-opaque-textview