2015-03-30 73 views
3

我遇到了一個問題,我正在使用一個ListView如何圓角的ListView及其子項像溢出:隱藏?

我已經定義了帶圓角的ListView。這ListView有一個標題和幾個項目。每個子視圖都有自己定義的background(只有顏色 - 標頭爲紫色,其他項目爲白色)。

ListView有一個background定義圓角。

問題是標題和其他子項目的background位於ListViewbackground之上。因此,我看不到ListView的圓形形狀。

我正在尋找一種方法來模仿CSS的overflow:hidden屬性,以便將項目和標題保留在ListView的圓角下。

有沒有解決方案?

+0

聽起來像你的列表視圖需要填充 – dhke 2015-03-30 18:52:40

回答

0

爲什麼不把列表視圖放在另一個佈局中,並給父項賦予圓角的背景資源?

<LinearLayout ... 
    android:background="@drawable/rounded_corners.xml" /> 
1

嘗試在ListView的元素添加一些填充有android:padding="...",但如果你的名單列有不同的背景(或漸變背景)會有這些項目,並將列表邊框之間的一些顏色差異。

或者,您可以嘗試在第一個列表行的頂角和最後一個列表行的底角使用邊界半徑。在覆蓋列表Adapter類的getView(...)方法時,可以設置正確的背景可繪製xml資源。

Ex。 first_row_bg.xml的(對於第一行):

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <gradient 
      android:angle="90" 
      android:startColor="#b3b3b3" 
      android:endColor="#f5f5f5" /> 
     <corners android:topLeftRadius="12dp" 
       android:topRightRadius="12dp"/> 
    </shape> 

例last_row_bg.xml的(最後一排):

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <gradient 
      android:angle="90" 
      android:startColor="#b3b3b3" 
      android:endColor="#f5f5f5" /> 
     <corners android:bottomLeftRadius="12dp" 
       android:bottomRightRadius="12dp"/> 
    </shape> 

例。 border_radius_bg.xml的(如果列表中只有一行):

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <gradient 
      android:angle="90" 
      android:startColor="#b3b3b3" 
      android:endColor="#f5f5f5" /> 
     <corners android:radius="12dp"/> 
    </shape> 

例。 gradient_bg.xml的(在中間行):getView(...)實現的

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
     <gradient 
      android:angle="90" 
      android:startColor="#b3b3b3" 
      android:endColor="#f5f5f5" /> 
    </shape> 

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     //inflate the layout (if necessary) and set the row content. 
     .... 

     if(data.length==1) { // if we have only one row 
      convertView.setBackgroundResource(R.drawable.border_radius_bg); 
     } else if(position==0) { // first row 
      convertView.setBackgroundResource(R.drawable.first_row_bg); 
     } else if(position==data.length-1) { // last row 
      convertView.setBackgroundResource(R.drawable.last_row_bg); 
     } else { // row in the middle 
      convertView.setBackgroundResource(R.drawable.gradient_bg); 
     } 
     return convertView; 
    } 

顯然,.xml文件應保存在「可繪製」目錄。