Gradient Background that doesn't scale

巧了我就是萌 提交于 2019-12-06 00:33:19
  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.

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

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!