Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

How to set transparency of a background image Android xml file

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

 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:

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

question from:https://stackoverflow.com/questions/22406961/how-to-set-transparency-of-a-background-image-android-xml-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can set the alpha property:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...