2017-08-14 95 views
0

你好,我试图将自定义形状和视图应用到我的android listview。当有足够的元素添加到列表视图中时,它可以正常工作,您必须滚动才能看到所有元素。但如果有小于它看起来像这样enter image description here将自定义形状应用到Android列表视图

<ListView 
    android:id="@+id/recipe_list_view" 
    android:layout_width="333dp" 
    android:layout_height="163dp" 
    android:layout_marginTop="46dp" 
    android:background="@drawable/shape" 
    android:divider="@color/darkblue" 
    android:dividerHeight="10.0sp" 
    android:gravity="center" 
    android:textAlignment="center" 
    app:layout_constraintLeft_toLeftOf="@+id/constraintLayout2" 
    app:layout_constraintRight_toRightOf="@+id/constraintLayout2" 
    app:layout_constraintTop_toBottomOf="@+id/imageView3"></ListView> 

这里是我的绘制

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#2ECC71" 
     android:endColor="#2ECC71" 
     android:angle="270"/> 

    <corners 
     android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

这里是代码实现列表视图

mListView = (ListView) findViewById(R.id.listView); 
      String[] listItems = new String[listOfUserIds.size()]; 

      for(int i = 0; i < users.size(); i++){ 
       listItems[i] = users.get(i); 
      } 

      ArrayAdapter adapter = new ArrayAdapter(EventDetailsActivity.this, android.R.layout.simple_list_item_1, listItems); 
      mListView.setAdapter(adapter); 

这里的形状看起来像什么时候有足够的元素滚动

enter image description here

+0

怎么样的画面错了吗?圆形正方形中间的那条线?有一个底部的事实?我可以看到这张照片,但我不知道你不喜欢哪部分。 –

+0

名称为 – hooray4horus

+0

的底部绿色块您可以发布它看起来不错时的外观截图吗?即一张填满的列表的截图。 –

回答

1

你给后台到ListView和列表视图高度163dp,所以如果只有几个项目也不会填写完整的布局。所以你可以看到背景。该解决方案是不给固定的高度,你可以给wrap_content或者不给列表的背景,你应该给列表项目。

+0

非常感谢你! – hooray4horus

0

Muthukrishnan的答案在技术上是正确的,但它似乎并没有真正解决“我怎么能有圆角的ListView?”的问题。

正如他所说的,您应该从ListView标记中移除背景,并将背景应用于您的项目视图。如果你这样做,你会(a)有一个方形列表视图或(b)每个项目都有圆角。

不幸的是,在没有提供背景的情况下,在ListView处没有很好的方法来提供圆角。

我能想到的最佳解决方案是在ListView之上叠加第二个View并使用此视图绘制圆角。我用一个矢量绘制来做到这一点。

下面是一个非常简单的应用程序,展示了这一点。

activity_main.xml中

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#777"> 

    <FrameLayout 
     android:layout_width="333dp" 
     android:layout_height="163dp" 
     android:layout_gravity="center"> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="#00ffffff" 
      android:dividerHeight="10dp"/> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:srcCompat="@drawable/fake_rounded_corners"/> 

    </FrameLayout> 

</FrameLayout> 

itemview.xml

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:gravity="center_vertical" 
    android:textColor="@color/colorPrimary" 
    android:textSize="18sp" 
    android:background="#fff"/> 

fake_rounded_corners.xml

<vector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="333dp" 
    android:height="163dp" 
    android:viewportWidth="333.0" 
    android:viewportHeight="163.0"> 

    <path 
     android:fillColor="#777" 
     android:pathData="M0 7v-7h7a7 7 0 0 0 -7 7"/> 

    <path 
     android:fillColor="#777" 
     android:pathData="M326 0h7v7a7 7 0 0 0 -7 -7"/> 

    <path 
     android:fillColor="#777" 
     android:pathData="M333 156v7h-7a7 7 0 0 0 7 -7"/> 

    <path 
     android:fillColor="#777" 
     android:pathData="M0 156v7h7a7 7 0 0 1 -7 -7"/> 

</vector> 
+0

太棒了!谢谢 – hooray4horus