2017-08-14 8 views
0

こんにちは、私はカスタムシェイプを適用してアンドロイドのリストビューに表示しようとしています。リストビューに十分な要素が追加されていて、すべてを見るためにスクロールしなければならない場合はうまく動作します。それはこのenter image description hereアンドロイドリストビューにカスタムシェイプを適用する

<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の上に第2のViewを重ねて、このビューを使用して角を描くことです。私はこれを行うためにベクトルdrawableを使いました。

これは実際にこれを示す非常に簡単なアプリです。

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

関連する問題