Gradient Background that doesn't scale

2019-05-10 22:53发布

I need to create a gradient that is 230dp high (i.e., not the whole screen), with the rest of the screen a solid color. I can't figure out how to do this -- everything I try ends up with the gradient expanding to fill the whole screen. My current XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:top="230dp">
        <shape android:shape="rectangle" >
            <color android:color="#333333" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <gradient
                android:angle="270"
                android:endColor="#333333"
                android:startColor="#D9D9D9"
                android:type="linear" />

            <size android:height="230dp" />
        </shape>
    </item>

</layer-list>

I then apply that drawable as the background to my LinearLayout in 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="fill_parent"
    android:background="#333333" >
    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:background="@drawable/grey_gradient_bg" >

        <!-- a bunch of content, buttons, etc. -->

    </LinearLayout>
</LinearLayout>

But the gradient expands to fill the whole screen.

I have tried using a PNG for the background, but I get the banding problems described here, and the fixes haven't helped me yet.

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-10 23:11
  1. Use a RelativeLayout instead of a LinearLayout
  2. Instead of using your gradient as a background of the main container of the layout, add a child View, set the height explicitly to the size you want (230dp), and set the background of that view to your gradient.
查看更多
等我变得足够好
3楼-- · 2019-05-10 23:20

Something like this might help you :)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/container"
   android:layout_width="fill_parent"
   android:layout_height="320dip"
   android:orientation="vertical"
   android:background="@drawable/grey_gradient_bg">
</LinearLayout>

The problem you encountered was because your layout had fill_parent on height.

Hope this will help you.

Good luck, Arkde

查看更多
在下西门庆
4楼-- · 2019-05-10 23:21

If your project doesn't require API < 21 you may set the size of your gradient directly in your drawable, like that:

 <item android:height="230dp">
    <shape android:shape="rectangle" >
        <gradient
            android:angle="270"
            android:endColor="#333333"
            android:startColor="#D9D9D9"
            android:type="linear" />
    </shape>
</item>

Anyway, to support API < 21 without this behavior, you may add this drawable and then, another drawable with the same name in the drawable-v21, but it without android:height="230dp".
Setting height in gradient tag or size really doesn't work.

查看更多
姐就是有狂的资本
5楼-- · 2019-05-10 23:23

Here's my solution using the other answers:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#333333" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:background="@drawable/grey_gradient_bg"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true" >

    <!-- all the content -->
    </RelativeLayout>
</RelativeLayout>

grey_gradient_bg is now simple:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270"
        android:endColor="#333333"
        android:startColor="#D9D9D9"
        android:type="linear" />

</shape>
查看更多
登录 后发表回答