2013-01-16 38 views
1

在Gmail應用程序的附加屏幕截圖中,ListView的背景似乎在列表右側有一個垂直漸變,與鄰近的View分開。Android:如何創建類似於gmail應用程序中的列表背景?

我知道我可以在xml中定義一個GradientDrawable,但是我怎樣才能用這個來繪製一個漸變作爲列表一側的背景,同時將列表的其他背景作爲淺灰色繪製?

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#DDDDDD" 
     android:endColor="#CCCCCC" 
     android:angle="0" /> 
</shape> 

enter image description here

+0

我剛剛張貼您的問題的解決方案。這將根據您的要求表現完全相同。 –

回答

0

我可以設置可繪爲95%淺灰色,在右邊一半的梯度,使用下面的:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#DDDDDD" 
     android:centerColor="#DDDDDD" 
     android:endColor="#CCCCCC" 
     android:centerX=".95" 
     android:centerY=".95" 
     android:angle="0" /> 
</shape> 
1

你只需指定您的cutsom繪製作爲您的ListView項的背景屬性創建。您可以在您用來定義列表元素的XML佈局中執行此操作。

舉個例子,我用這個佈局(list_item.xml)定義爲我的列表項的自定義佈局(並分配一個漸變作爲背景):

<?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="wrap_content" 
    android:background="@drawable/list_item_gray_gradient" 
    android:orientation="horizontal" 

... 
+0

漸變的大小將不正確。漸變將沿着列表視圖的整個寬度發生,而不是僅沿着右側。 – ab11

+0

......明白了,又增加了一個答案。 – Booger

1

您可以創建一個可繪製,並將其放置在包含列表項目的佈局的右側。因此,您的漸變是您視圖中的一個獨立組件,它將您的ListView展示給右側的視圖。

像這樣的僞代碼

<LinearLayout 
    android:orientation="horizontal" 

    <ListView 

    /> 

    <ImageView 
     android:background="@drawable/gradient_drawable" 
    > 


> 
+0

我認爲漸變應該是列表背景的一部分,以便列表分隔線在漸變頂部繪製。如果我將它放在列表的右側,單個列表項將在漸變開始之前結束。 – ab11

+0

不一定,你可以使用消極的對齊屬性(即android:layout_marginRight =「 - 2dp」來對齊它,如你所願; – Booger

+0

啊,這是一個合理的想法,我剛剛找到了一個解決方案,它似乎有點清潔 – ab11

0

首先創建此drawable(可拉伸/ list_gray_gradient.xml)

<?xml version="1.0" encoding="utf-8"?> 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- This is the line --> 
<item> 
     <shape> 
      <solid android:color="#CCCCCC" /> 
     </shape> 
</item> 
<!-- This is the main color --> 
<item android:right="10dp"> 
    <shape> 
      <solid android:color="#DDDDDD" /> 
    </shape> 
</item> 

</layer-list> 

,然後只用給你的list item layout(佈局/ list_item.xml)

<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"    
     android:background="@drawable/list_gray_gradient" > 
</RelativeLayout> 
相關問題