簡體   English   中英

如何設置背景圖片Android xml文件的透明度

[英]How to set transparency of a background image Android xml file

我已經使用以下代碼行將圖像作為我的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完成。 它是一個非常古老的帖子,但我認為我應該回答,可能對某人有幫助。

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

這就是您的布局現在的樣子:

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

        </bitmap>
    </item>

</layer-list >

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

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之間)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM