2017-08-14 85 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