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

use*_*962 28 xml android android-layout

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

 android:background="@drawable/background"
Run Code Online (Sandbox Code Playgroud)

我现在想让它透明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>
Run Code Online (Sandbox Code Playgroud)

Dec*_*coy 41

您可以设置alpha属性:

android:alpha="0.4"
Run Code Online (Sandbox Code Playgroud)

通过代码:

yourView.setAlpha(0.5f);
Run Code Online (Sandbox Code Playgroud)

只有背景:

yourView.getBackground().setAlpha(120);  // here the value is an integer not float
Run Code Online (Sandbox Code Playgroud)

  • 我发现使用它有问题,因为我在这个页面上的文字也褪色了,我不想要,还有另外一种方法吗? (2认同)

Mon*_*ble 18

它可以使用自定义drawable完成.它是一个非常古老的帖子,但我认为我应该回答,可能对某人有帮助.

  1. 使用位图创建一个可绘制的xml资源,根据bg.xml说.
  2. android:src ="@ drawable/background"设置为您想要作为背景的图像.
  3. 根据所需的不透明度将android:alpha ="0.4"设置为0到1之间的任何值.
  4. 现在将根布局的背景设置为新创建的可绘制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"/>
Run Code Online (Sandbox Code Playgroud)

这就是您的布局现在的样子:

<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>
Run Code Online (Sandbox Code Playgroud)


Pra*_*arg 13

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

 android:alpha="0.x"
Run Code Online (Sandbox Code Playgroud)

将导致整个屏幕(子视图)淡出.相反,你可以利用

android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"
Run Code Online (Sandbox Code Playgroud)

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


小智 11

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

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 >
Run Code Online (Sandbox Code Playgroud)