我需要创建一个230dp高的渐变(即,不是整个屏幕),屏幕的其余部分是纯色 . 我无法弄清楚如何做到这一点 - 我尝试的一切都以渐变扩展来填满整个屏幕 . 我当前的XML看起来像这样:
<?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>
然后我将drawable作为背景应用于XML中的LinearLayout:
<?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>
但渐变扩展以填满整个屏幕 .
我尝试使用PNG作为背景,但我得到the banding problems described here,修复程序还没有帮助我 .
4 回答
像这样的东西可能会帮助你:)
您遇到的问题是因为您的布局在高度上有
fill_parent
.希望这会帮助你 .
祝你好运,Arkde
使用RelativeLayout而不是LinearLayout
不使用渐变作为布局主容器的背景,而是添加子视图,将高度显式设置为所需大小(230dp),并将该视图的背景设置为渐变 .
如果您的项目不需要API <21,您可以直接在drawable中设置渐变的大小,如下所示:
无论如何,为了支持没有这种行为的API <21,你可以添加这个drawable然后,另一个drawable-v21中具有相同名称的drawable,但它没有android:height = "230dp" .
在渐变标签或尺寸中设置高度确实不起作用 .
这是我使用其他答案的解决方案:
grey_gradient_bg现在很简单: