2015-03-30 37 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文件应保存在“可绘制”目录。