53

ListView のみを含む単純なレイアウトを作成すると、最後の項目の後にセパレーターが表示されず、少し見苦しくなります。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
</RelativeLayout>

ここに画像の説明を入力

しかし、リストビューの下に別のビューを追加し、リストビューの属性を設定すると、最後の項目の後にセパレーターが表示されることがわかりました。android:layout_above

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_blue_dark"
        android:text="Bottom" />
</RelativeLayout>

ここに画像の説明を入力

リストビューがこのように動作するのはなぜですか? リストビューのみを含むレイアウトの最後の項目の後にセパレータを取得するにはどうすればよいですか?

4

6 に答える 6

102

答えは非常にandroid:layout_height="wrap_content"簡単android:layout_height="match_parent"ですListView

なぜこれが起こるのか、おそらく推測できます。

于 2014-01-08T17:13:42.367 に答える
15

これを試しましたか?

android:footerDividersEnabled="true"

そうでない場合は、これを試してください

<View
android:background="#00ff00"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/YOUR_LIST_ID" />
于 2013-01-07T16:06:42.183 に答える
1

この問題を回避するハックを用意しました。最後のセパレーターは別のビューがリスト ビューの後に続く場合にのみ表示されるため、2 番目のビューを に設定することで非表示にすることができlayout_heightます0dp

これはまだハックですが、最後の仕切りが他の仕切りと一致するように見えるので、適切な色と寸法を推測して水平線を手動で作成することをお勧めします.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/invisible"
        android:layout_alignParentTop="true" />

    <View
        android:id="@+id/invisible"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true" />    
</RelativeLayout>
于 2013-01-07T17:59:18.520 に答える
1

@Natix の説明と同様のことを行いますが、リストに含まれるレイアウトをいじる代わりに、ListView.addFooterView() を介して空のビューをリストのフッターとして追加するだけです。

于 2013-04-30T18:44:04.780 に答える
0

そのようにコーディングすると、リストビューが表示されたときに多少異なるように見えます。しかし、あなたもこれに従うことができます。

まず、という名前のスタイルを定義しますLine

<style name="Line">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">2px</item>
    <item name="android:background">@color/white</item>
</style>

// 上記のスタイルでは、必要に応じて線の高さを変更できます。

上記の行を使用する場所はどこでも、xml ファイルで次のように宣言できます。

<LinearLayout
android:orientation = "vertical"
.......................
............................>
 <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />


<View
        style="@style/Line"/>

</LinearLayout>

上記のコードは、リストビューの下に行を作成します。上記のコードは、プロジェクトのさまざまな場所で使用する場合に最も役立ちます。または、1 か所だけにしたい場合は、このようにすることもできます。

 <LinearLayout
    android:orientation = "vertical"
    .......................
    ............................>
     <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             />
<View
    android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="#20b2aa" />

</LinearLayout>

お役に立てれば。ところで、理由はあいまいです。一度でもこれを検索して、上記で説明したこの方法に従いました。

于 2013-01-07T16:07:29.120 に答える