【问题标题】:Gradient Background that doesn't scale不缩放的渐变背景
【发布时间】:2012-03-13 16:03:44
【问题描述】:

我需要创建一个 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 作为背景,但我得到了the banding problems described here,并且修复还没有帮助到我。

【问题讨论】:

  • 如果您的 LinearLayout 是另一个布局的子级,那么父级可能没有像您期望的那样拉伸。您应该提供整个布局 xlm 让我们分析它。

标签: android background gradient


【解决方案1】:
  1. 使用 RelativeLayout 而不是 LinearLayout
  2. 不要使用渐变作为布局主容器的背景,而是添加一个子视图,将高度显式设置为所需的大小 (230dp),然后将该视图的背景设置为渐变。李>

【讨论】:

  • 啊,我明白了。我可以有一个 separate RelativeLayout,它只保留我的渐变背景,并设置为明确的高度。那么我的内容就可以活在一个LinearLayout中,两者都可以设置为layout_alignParentTop="true"来解决问题。
【解决方案2】:

如果您的项目不需要 API

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

无论如何,要支持 API 在渐变标签或大小中设置高度确实不起作用。

【讨论】:

    【解决方案3】:

    这样的事情可能会对你有所帮助:)

    <?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>
    

    您遇到的问题是因为您的布局在高度上有fill_parent

    希望这会对你有所帮助。

    祝你好运, 方舟

    【讨论】:

    • 问题是我的LinearLayout需要扩展以包含内容,这是一个全屏高。
    • 好吧,你的父母LinearLayout,背景为android:background="#333333",将填满整个屏幕,但如果我理解你的问题,你只希望屏幕的一部分包含渐变,即 320dp 高.为此,您只需将android:id="@+id/container" 的高度设置为 320dp。我对吗?还是我误解了你的问题?
    • (我最初的问题并不清楚,因为我遗漏了 LinearLayout 中的内容。)
    • 我们的 cmets 在那里被越过了...我的 android:id="@+id/container" 中的所有内容都需要扩展以填满屏幕。我认为从您的回答和 Rich 的回答中,我明白我现在需要做什么。
    【解决方案4】:

    这是我使用其他答案的解决方案:

    <?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 现在很简单:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多