30

ListView 項目にマージンを追加しようとしています (無駄に)。以下の RelativeLayout にマージン値を追加しようとしましたが、何をしても、各アイテム間に 1px の行しかないようです。

私が本当に望むのは、各アイテムの角を丸くし、1 ピクセルの黒い境界線と、左右に 3 ~ 5 ピクセルのマージンを持たせることですが、今は各アイテムの周りのマージンだけで解決します :-)

どうすれば目標を達成できますか? 今のところちょうどマージン... ;-)

ここに私が持っているものがあります:

更新:以下の xml を更新して、メイン レイアウトとフラグメント レイアウトを削除しました。また、ListView アイテムのレイアウトを現在のものに更新しました。これは、私が望むものに近いですが、まだ完全ではありません。スクリーンショットも追加

リストビュー アイテム レイアウト xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/matchMargin"
android:paddingRight="@dimen/matchMargin"
android:paddingTop="@dimen/matchMargin" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#cfcfcfcf" >

    <include
        android:id="@+id/matchKampstart"
        layout="@layout/kampstart_layout" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/matchKampstart"
        android:layout_marginTop="@dimen/belowKampstartMargin" >

        <ImageView
            android:id="@+id/tournamentImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/tournamentImageViewContentDescription"
            android:gravity="left"
            android:src="@drawable/sofabold_launcher" />

        <ImageView
            android:id="@+id/homeTeamImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/homeTeamImageViewContentDescription"
            android:src="@drawable/sofabold_launcher" />

        <TextView
            android:id="@+id/homeTeam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:text="@string/home"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:gravity="center"
            android:text="@string/dash"
            android:textSize="12sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/awayTeamImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="2dp"
            android:adjustViewBounds="true"
            android:contentDescription="@string/awayTeamImageViewContentDescription"
            android:src="@drawable/sofabold_launcher" />

        <TextView
            android:id="@+id/awayTeam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="@string/away"
            android:textSize="14sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/matchKampstart"
        android:layout_marginTop="@dimen/belowKampstartMargin" >

        <ImageView
            android:id="@+id/tvChannelImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="false"
            android:contentDescription="@string/tvChannelImageViewContentDescription"
            android:gravity="right"
            android:src="@drawable/sofabold_launcher" />
    </LinearLayout>

</RelativeLayout>

</RelativeLayout>

これにより、次のようになります。各項目の左右に非常に小さな線があることに気付くでしょう。それは私も取り除きたいです。

ここに画像の説明を入力

4

5 に答える 5

71

私はレイアウトが得意ではありませんが、過去に、ListView の行が LayoutParams を無視することが多いことに気付きました。これがどこで発生するのか、またはオーバーライドできるかどうかはわかりませんが、別のレイアウトを追加することで簡単に回避できることはわかっています。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="#990000ff" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#9900ff00"
        android:paddingLeft="10dp" >

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#99ff0000" />
    </LinearLayout>

</LinearLayout>

通常、子が 1 つしかないレイアウトは削除できますが、ご覧のとおり、これは目的を果たします。

スクリーンショット

最も外側のレイアウトは青、TextView は赤、緑は余分なスペースを追加できる特別なレイアウトです。パディング (左の緑) とマージン (右の緑なし) の違いに注意してください。マージン( )を使用することを明確に述べていますandroid:layout_marginが、コードでは明らかにパディング(android:padding)を使用しているため、両方を含めました。

于 2013-03-11T17:12:30.387 に答える
12

理由については、こちらの回答を参照してください。つまり、子は親に質問し、リスト ビューの行AbsListView.LayoutParamsは余白を含まない を使用します。

ListView の行ビューとして使用すると、LinearLayout のマージンが無視される理由

于 2013-08-17T16:22:21.040 に答える
4

アダプターで、 getView() で相対的なレイアウトをキャッチし、レイアウト params を指定します。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom
YourRelativeLayoutInAdapter.setLayoutParams(params);
于 2013-03-11T15:42:33.263 に答える