2012-03-13 27 views
3

230dpの高さ(つまり、画面全体ではない)の勾配を作成する必要があります。残りの画面は単色です。私はこれを行う方法を理解することができません - 私はすべての画面を埋めるために拡大するグラデーションに終わる試してみましょう。勾配のない勾配

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

しかし、勾配は、画面全体を埋めるために拡張されます。私は、XMLの私のLinearLayoutの背景として、その描画可能に適用

<?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は次のようになります。

私はバックグラウンドにPNGを使用しようとしましたが、the banding problems described hereを取得しましたが、この修正はまだ手伝ってくれませんでした。

+0

あなたのLinearLayoutは多分別のレイアウトの子である場合親はあなたが期待しているように伸びていません。レイアウトxlm全体を提供して解析してください。 – woodshy

+0

完全なレイアウトXMLで編集しました。 – matt

答えて

3
  1. 、代わりに、レイアウトのメインコンテナの背景として、あなたのグラデーションを使用する代わりのLinearLayout
  2. のRelativeLayoutを使用して、子ビューを追加し、あなたが希望のサイズ(230dp)に明示的に高さを設定しますそのビューの背景をグラデーションに設定します。
+1

ああ、私は今それを得る。私は_separate_ RelativeLayoutを持つことができます。これは何もしませんが、グラデーションの背景を保持し、明示的な高さに設定されています。私のコンテンツはLinearLayoutに保存され、問題を解決するにはlayout_alignParentTop = "true"に設定することができます。 – matt

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

+0

親 'LinearLayout'は、背景が' android:background = "#333333" 'で、画面全体を塗りつぶしますが、あなたの質問が分かっていれば、画面の一部だけにグラデーションが含まれ、 。そしてそれを行うには、あなたの 'android:id =" @ + id/container " "の高さを320dpに設定するだけです。私は正しい?または私はあなたの質問を誤解しましたか? – Arkde

+0

(私はLinearLayoutにコンテンツを残していたので、私の元の質問からは分かりませんでした) – matt

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>