不缩放的渐变背景

2024-06-23

我需要创建一个 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>

然后,我将该可绘制对象作为背景应用到 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 作为背景,但我得到这里描述的条带问题 https://stackoverflow.com/questions/2928101/android-using-linear-gradient-as-background-looks-banded,并且修复还没有帮助我。


  1. 使用RelativeLayout代替LinearLayout
  2. 不要使用渐变作为布局主容器的背景,而是添加一个子视图,将高度显式设置为所需的大小 (230dp),然后将该视图的背景设置为渐变。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不缩放的渐变背景 的相关文章

随机推荐