I am having trouble applying a gradient background to a LinearLayout.
This should be relatively simple from what I have read but it just doesn't seem to work. For reference sakes I am developing on 2.1-update1.
header_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear"/>
</shape>
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/header_bg">
</LinearLayout>
If I change @drawable/header_bg to a color - e.g. #FF0000 it works perfectly fine. Am I missing something obvious here?
Ok I have managed to solve this using a selector. See code below:
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/main_header_selector">
</LinearLayout>
main_header_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
Hopefully this helps someone who has the same problem.
It is also possible to have the third color (center). And different kinds of shapes.
For example in drawable/gradient.xml:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#000000"
android:centerColor="#5b5b5b"
android:endColor="#000000"
android:angle="0" />
</shape>
This gives you black - gray - black (left to right) which is my favorite dark background atm.
Remember to add gradient.xml as background in your layout xml:
android:background="@drawable/gradient"
It is also possible to rotate, with:
angle="0"
gives you a vertical line
and with
angle="90"
gives you a horizontal line
Possible angles are:
0, 90, 180, 270.
Also there are few different kind of shapes:
android:shape="rectangle"
Rounded shape:
android:shape="oval"
and problably a few more.
Hope it helps, cheers!
In XML Drawable File:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="90"
android:endColor="#9b0493"
android:startColor="#38068f"
android:type="linear" />
</shape>
</item>
</selector>
In your layout file: android:background="@drawable/gradient_background"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
android:orientation="vertical"
android:padding="20dp">
.....
</LinearLayout>
Try removing android:gradientRadius="90". Here is one that works for me:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="@color/purple"
android:endColor="@color/pink"
android:angle="270" />
</shape>
My problem was the .xml extension was not added to the filename of the newly created XML file. Adding the .xml extension fixed my problem.
I don't know if this will help anybody, but my problem was I was trying to set the gradient to the "src" property of an ImageView like so:
<ImageView
android:id="@+id/imgToast"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="@drawable/toast_bg"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
Not 100% sure why that didn't work, but now I changed it and put the drawable in the "background" property of the ImageView's parent, which is a RelativeLayout in my case, like so: (this worked successfully)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/custom_toast_layout_id"
android:layout_height="match_parent"
android:background="@drawable/toast_bg">
I would check your alpha channel on your gradient colors. For me, when I was testing my code out I had the alpha channel set wrong on the colors and it did not work for me. Once I got the alpha channel set it all worked!
来源:https://stackoverflow.com/questions/5976805/android-linearlayout-gradient-background