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

时间:2014-03-14 13:52:26

标签: android xml android-layout

我已将图像作为我的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>

9 个答案:

答案 0 :(得分:38)

您可以设置alpha属性:

android:alpha="0.4"

通过代码:

yourView.setAlpha(0.5f);

只有背景:

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

答案 1 :(得分:17)

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

  1. 使用位图创建一个可绘制的xml资源,如根 bg.xml
  2. android:src =&#34; @ drawable / background&#34; 设置为您想要作为背景的图片。
  3. 根据所需的不透明度将 android:alpha =&#34; 0.4&#34; 设置为0到1之间的任何值。
  4. 现在将根布局的背景设置为新创建的可绘制xml资源,即 bg.xml 。这只会改变布局背景图像的不透明度,而不会影响布局子元素的不透明度。
  5. 资源:
    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>
    

答案 2 :(得分:10)

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

 android:alpha="0.x"

会导致整个屏幕(子视图)淡出。 相反,你可以使用

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

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

答案 3 :(得分:8)

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

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 >

答案 4 :(得分:2)

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

android:alpha="0.4"

答案 5 :(得分:2)

尝试使用Drawable.setAlpha();

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

答案 6 :(得分:2)

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

答案 7 :(得分:2)

您可以使用alpha属性 机器人:阿尔法=&#34; 0.5&#34 ;; (0到1之间)

答案 8 :(得分:0)

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