J'ai mis une image en arrière-plan de mon Android avec la ligne de code suivante:
Android:background="@drawable/background"
Je veux maintenant le rendre transparent de 40%, mais comment est-ce possible dans le fichier xml? Mon fichier exm est illustré ci-dessous:
<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>
Vous pouvez définir la propriété alpha:
Android:alpha="0.4"
via le code:
yourView.setAlpha(0.5f);
seul l'arrière-plan:
yourView.getBackground().setAlpha(120); // here the value is an integer not float
Cela peut être fait en utilisant un dessin personnalisé. C'est un très vieux post mais j'ai pensé que je devrais répondre, cela pourrait être utile pour quelqu'un.
Ressources :
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"/>
Voici à quoi ressemblera votre mise en page maintenant:
<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>
Si vous avez défini une image comme arrière-plan,
Android:alpha="0.x"
provoquera un fondu de l'écran entier (vues enfants). Au lieu de cela, vous pouvez utiliser
Android:backgroundTint="#80FFFFFF"
Android:backgroundTintMode="src_over"
pour estomper uniquement l'image d'arrière-plan sans affecter les vues enfant.
vous pouvez créer une ressource de liste de couches et la définir comme propriété d'arrière-plan de votre vue (mise en page)
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 >
Vous pouvez utiliser la propriété alpha Android: alpha = "0.5"; (Entre 0 et 1)
définir dans votre fichier xml
Android:alpha="0.4"
qui sera varie de 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>
Vous pouvez utiliser la propriété alpha
( http://developer.Android.com/reference/Android/view/View.html#attr_Android:alpha ) pour votre View
:
Android:alpha="0.4"
Essayez d'utiliser Drawable.setAlpha ();
View backgroundImage = findViewById(R.id.background_image);
Drawable background = backgroundImage.getBackground();
background.setAlpha(80);