4

ヘッダーにマージンを適用せずに、ListView にマージン/パディングを設定することは可能ですか? ListView を Google Now のレイアウトのようにしたい。この小さな問題を除いて、すべてが機能しています。

質問: ListView のレイアウト パラメータをヘッダーに適用しないようにすることは可能ですか?

EDIT1:詳細

紫色のビューは、listView のヘッダーです。リスト項目のレイアウトにマージンを追加しようとしましたが、これもうまくいきません。必要なのは、リストビューのヘッダーにマージンを適用しないように、リストビューにマージンを適用する方法だけです。

EDIT2: 私のレイアウト header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/top_height"
        android:background="@color/top_item" />

    <View
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sticky_height"
        android:layout_gravity="bottom" />

</FrameLayout>

root_list.xml

<LinearLayout>
<com.test.CustomListView
    android:id="@android:id/list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_height="wrap_content" /></LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview_note"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview_note_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/></LinearLayout>

この投稿が長くなったため、ここでは不要なレイアウト プロパティを削除しました。 見た目はこんな感じ

4

4 に答える 4

7

いろいろ調べた結果、listView レイアウト パラメータをヘッダーから分離することは不可能であり、ヘッダーは listView の一部であることがわかりました。したがって、望ましいマージン効果を得るために、多くのことを見回した後、リストビューアイテムのレイアウトのルートにマージンを追加しても機能しないことがわかりました。したがって、既存のルート LinearLayout(ネストされた LinearLayout) 内に別のダミー LinearLayout を追加し、ネストされたレイアウト内に子を追加しました。内部レイアウトにマージンを設定すると、目的の効果が得られます。

コード:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:background="@drawable/list_selector"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textview_note"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textview_note_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
于 2012-12-27T12:32:32.933 に答える
0

ヘッダー部分に、次のタグを追加します。

android:layout_alignParentLeft="true"

これはあなたの問題を解決します。

于 2012-12-27T07:53:36.673 に答える
-1

リストビューのレイアウトファイルにマージンを与えることでそれを行うことができ、出力が得られます。

于 2012-12-27T07:40:27.800 に答える