4

次の RelativeLayout をリスト ビュー アイテムとして検討してください。

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/foo"
        android:layout_alignLeft="@id/foo"
        android:text="bar"/>

</RelativeLayout>

hierarchyviewer(Android JB/API 17を搭載したデバイスで)調べたところ、bar高さが0になりました。

編集:期待される結果:期待される結果

質問:そのような相対的なレイアウト動作の説明とfoo、 要件を満たすレイアウトを実現するためにレイアウトを修正する方法は何ですか?bigfoobarfoo

4

2 に答える 2

1

2 つのオプションが表示されます。

オプション 1、ネストなし (この方法では、バーはレイアウトの上部にありますが、レイアウトの使用に適している場合は、上部マージンを設定して少し下げることができます):

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:text="bar"/>

</RelativeLayout>

オプション 2、最初の RelativeLayout に別の RelativeLayout をネストする:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="bigfoo"
        android:textSize="60sp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/bigfoo"
        android:layout_alignBottom="@id/bigfoo"
        android:orientation="vertical"
        android:layout_toRightOf="@id/bigfoo">

        <TextView
            android:id="@+id/foo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="foo" />

        <TextView
            android:id="@+id/bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/foo"
            android:text="bar" />
    </RelativeLayout>
</RelativeLayout>

オプション 2 を使用すると、期待どおりの結果が得られるはずですが、別のレイアウト内にレイアウトをネストするという犠牲が伴います。UI の残りの部分が軽い場合、問題にはなりません ;)

編集:これを試していただけますか?

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

    <TextView
        android:id="@+id/bigfoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="bigfoo"
        android:textSize="60sp"/>

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bigfoo"
        android:layout_centerVertical="true"
        android:text="foo"/>

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/foo"
        android:layout_alignLeft="@id/foo"
        android:text="bar"/>

</RelativeLayout>

これが私が見ているものですが、これが機能する理由を説明するのは難しいでしょう... ここに画像の説明を入力

于 2013-10-28T14:17:51.137 に答える