2011-07-11 47 views
2

我有ListView,圓角實現像波紋管。Android:ListView,圓角問題

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/list_bg" 
    android:divider="#dbdbdb" 
    android:dividerHeight="1sp" 
    android:cacheColorHint="#00000000" 
    android:descendantFocusability="afterDescendants" 
    android:scrollbars="vertical"> 
</ListView> 

其中list_bg.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#ffffff"/> 
    <corners android:bottomLeftRadius="10dp" 
      android:bottomRightRadius="10dp" 
      android:topLeftRadius="10dp" 
      android:topRightRadius="10dp" 
      /> 
</shape> 

這給我圓角。 問題是每個項目都是RelativeLayout,左邊是TextView,右邊是ImageButton。這兩個視圖都具有按下,保存和默認狀態的自定義圖形。像這樣的東西。

+------------------------------------+ 
|  TextView  | ImageButton | 
+------------------------------------+ 

問題是自定義圖形覆蓋ListView背景,因此在第一個和最後一個項目上的圓角。滾動時一切都很好。

我已經編寫了自定義適配器,並在getView方法中爲物品設置了正確的bg。

@Override 
public View getView(int position, View view, ViewGroup parent) {   
    ... 
    if(position == 0) { 
     mItemView.setBackgroundResource(R.drawable.cell_item_first_selector); 
     mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector); 
    } 
    else if (position == mData.size() -1) { 
     mItemView.setBackgroundResource(R.drawable.cell_item_last_selector); 
     mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector); 
    } 
    else { 
     mItemView.setBackgroundResource(R.drawable.cell_item_def_selector); 
     mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector); 
    } 
    ... 
} 

它的作品,但我不知道這是很好的解決方案。有沒有其他方法可以自動在Android視圖中四捨五入,而不僅僅是通過設置背景?

回答

0

那麼,如果您需要此列表中的項目的背景,您應該將背景附加到項目佈局,而不是附加到列表視圖。

+0

它沒有幫助。仍然第一個和最後一個項目圖形隱藏圓角。是否有唯一的解決方案爲第一個和最後一個項目設置不同的圖形 – Adrian

+0

嗯,它應該工作。你能發佈一些更多的佈局代碼嗎? –

2
public class ClippedListView extends ListView { 

/** 
* @param context 
*/ 
public ClippedListView(Context context) { 
    super(context); 
} 

/** 
* @param context 
* @param attrs 
*/ 
public ClippedListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    float radius = 10.0f; 
    Path clipPath = new Path(); 
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); 
    clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); 
    canvas.clipPath(clipPath); 
    super.dispatchDraw(canvas); 
} 
} 

第一卡不能正常工作,然後使用

setLayerType(View.LAYER_TYPE_SOFTWARE,NULL)

我clippedListView解決問題。我的物品背景不再與我的角落混淆!

+0

輝煌的解決方案。保存了我的一天。你有沒有任何想法沒有SetLayerType(它需要API 11)如何實現? – Kenneth