26

要するに、他の子供がどのように振る舞うかに関係なくRelativeLayout、常にその高さに一致するように子供に指示することは可能ですか? RelativeLayoutRelativeLayout要するに、それだけです。詳細は以下。

私が達成しようとしているのは、ListView少なくとも 3 つのビューを含む行です。これらのビューの 1 つは、リスト エントリの右側にストライプのようなものになります (下の赤いビュー)。私が抱えている問題はTextView、左側に があり、テキストの量によっては、ストライプがレイアウト全体を埋められないことです。これは、以下の画像で非常に明確に説明されています。

ListViewアイテムのレイアウト:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

<View
        android:id="@+id/bottom_line"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_alignParentBottom="true"
        android:background="#fc0" />

<!-- Why match_parent does not work in view below? -->
<View
        android:id="@+id/stripe"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:background="#f00" />

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/stripe"
        android:text="This is a very long line, meant to completely break the match_parent property of the box at right"
        style="?android:textAppearanceLarge"/>

</RelativeLayout>

結果:

子がレイアウト全体を垂直方向に埋めない RelativeLayout

ストライプとルートの高さを に設定しmatch_parentても違いはありません。やったよ。

質問を繰り返しますが、赤いストライプが常に親を垂直に埋めてほしいです。ストライプがルートの上部に整列していないことがわかります。

注: 上記の例は、私が考えることができる最も単純で自己完結型の例です。これは、静的配列によって設定されListActivityた匿名の単なるです。関連するコードは最大で 8 行なので、心配はいりません。まさにレイアウトです。その上、私はこれをネストされたs で既に動作させていますが、可能であれば、レイアウト構造の深さを少し減らそうとしています。期待どおり、正常に動作します。ArrayAdapterStringonCreateLinearLayoutLinearLayout

4

5 に答える 5

35

同じ要件があり、私の解決策は、赤いストライプの上部を親の上部に揃え、ストライプの下部を左側のテキスト ビューの下部に揃えることでした。この場合の高さは関係ありません。wrap_contentまたはのいずれかを使用できますmatch_parent

于 2013-08-18T20:43:33.047 に答える
19

メインRelativeLayoutを 内に配置するだけLinearLayoutで、子ビューの高さの計算が正しくなります。私は同じ問題を抱えていましたが、それは私にとってはうまくいきました。

于 2015-03-01T21:21:40.453 に答える
6

これを試して:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

    <View
        android:id="@+id/stripe"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/text"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/text"
        android:background="#f00"
        android:minHeight="50dp"/>

    <TextView
        android:id="@+id/text"
        style="?android:textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/stripe"
        android:text="This is a very long line, meant to completely break the match_parent property of the box at right"/>

    <View
        android:id="@+id/bottom_line"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_below="@id/text"
        android:background="#fc0"/>

</RelativeLayout>

赤いビューの上下の位置合わせは、テキストビューと同じになりました。

于 2013-08-18T20:52:31.530 に答える
1

これは厄介な問題であり、RelativeLayout を LinearLayout 内に配置する必要があります。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="40dp"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:id="@+id/deleteItem"
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:clickable="true"
                android:background="@color/background_grey"
                >
                <TextView
                    style="@style/SmallButtonFont"
                    android:layout_marginRight="12dp"
                    android:layout_centerInParent="true"
                    android:text="CLEAR"
                    android:textColor="@color/globalRedLight" />
            </RelativeLayout>
    </RelativeLayout>
</LinearLayout>
于 2016-11-12T14:40:58.903 に答える