2012-03-13 56 views
3

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

我然后应用绘制为背景,以我的LinearLayout在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> 

但梯度扩展以填充整个屏幕。

我尝试过使用PNG作为背景,但是我得到了the banding problems described here,并且修复程序还没有帮助我。

+0

如果你的LinearLayout是另一个布局的孩子那么也许父母并不像你期待的那样伸展。你应该提供整个布局xlm让我们分析它。 – woodshy 2012-03-13 16:08:51

+0

使用完整版式XML进行编辑。 – matt 2012-03-13 16:36:15

回答

3
  1. 使用RelativeLayout的,而不是一个的LinearLayout
  2. 而不是使用您的梯度布局的主要容器的背景下,添加一个子视图,明确地将高度设置为你想要的大小(230dp)并将该视图的背景设置为您的渐变。
+1

啊,我现在明白了。我可以有一个_separate_ RelativeLayout,它除了保存我的渐变背景外,什么都不做,并且被设置为一个明确的高度。然后,我的内容可以存在于LinearLayout中,并且两者都可以设置为layout_alignParentTop =“true”来解决问题。 – matt 2012-03-13 16:48:06

1

像这样的东西可以帮助你:)

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

希望这会帮助你。

祝你好运, Arkde

+0

问题是,我的LinearLayout需要扩展以包含内容,这是一个全屏高。 – matt 2012-03-13 16:40:18

+0

你的父母'LinearLayout',具有背景'android:background =“#333333”',将填满整个屏幕,但是如果我理解你的问题,你只想要屏幕的一部分包含渐变,那是320dp高。为此,您只需将您的'android:id =“@ + id/container” '的高度设置为320dp。我对吗?或者我误解了你的问题? – Arkde 2012-03-13 16:46:27

+0

(这是从我原来的问题不清楚,因为我遗漏了LinearLayout中的内容。) – matt 2012-03-13 16:48:30

0

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

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