2011-03-30 208 views
0

請考慮我一個Android noob;我試圖創建一個自定義ListView,應該看起來像這樣('這是一個自定義ListView實現在BlackBerry中,但我想創建Android上相同的外觀和感覺:):Custom ListView with rounded corners, implemented on BlackBerryAndroid:自定義列表視圖繪圖

我'目前已經提出了以下的Android代碼和XML,但它不會改變標準的ListView的外觀:

代碼:

public class RoundedListView extends ListView 
{ 
    public RoundedListView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public RoundedListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public RoundedListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    public void onDraw(Canvas canvas) 
    {  
     Paint paint = new Paint(); 
     paint.setColor(Color.CYAN); 

     canvas.drawRect(10, 10, 10, 10, paint); 
     canvas.drawColor(Color.YELLOW); 

     super.onDraw(canvas); 
    } 
} 

的XML(main.xml中):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <com.gravityzoo.android.containers.RoundedListView 
     android:layout_height="wrap_content" 
     android:id="@+id/listView1" 
     android:layout_width="match_parent"> 
    </com.gravityzoo.android.containers.RoundedListView> 
</LinearLayout> 

有誰知道如何使這個簡單的繪圖功能工作? 在此先感謝!

回答

2

Android與Blackberry完全不同。

在Android中,您需要設置ListView項目的背景和邊距以實現上述效果。通常沒有必要與像ListViewTextView視圖層次的「正常」的成員組合定製拉絲等

經常在黑莓的發展,你會繼承框架場只是爲它提供一個自定義的寬度和高度。在Android中,您可以使用簡單的「合成」來完成此操作;換句話說,實例化一個組件然後在其上設置屬性而不需要子類化。

另一個重要的點是ListView傾向於同質數據,其中所有的行都遵循相同的一般格式或一小組格式。 如果您只有四個項目,並且每個項目具有不同的特徵,那麼我建議您使用一個普通的舊的LinearLayout,這將允許您垂直放置您的視圖。這將爲您節省相當多的工作時間,將您的四個視圖放進一個適配器,並讓您最大限度地控制每個項目的顯示方式。