如何在RelativeLayout中设置相同的高度?

时间:2012-09-18 08:22:57

标签: android relativelayout

以下是我的xml代码:

<RelativeLayout
    android:id="@+id/ib"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center" >

    <ImageButton
        android:id="@+id/button_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/ib"
        android:gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

ImageButton ib和TextView tw具有不同的高度 我希望ib和电视具有相同的高度 那就是如果ib的高度>电视,然后设置电视的高度与ib相同。
如果不然,那么将ib的高度设置为与tv相同 我该怎么做?

4 个答案:

答案 0 :(得分:12)

android:layout_alignBottom="@+id/button_icon"属性添加到textview

答案 1 :(得分:3)

使用LinearLayout并将ImageButton高度设置为"wrap_content",将TextView设置高度设置为"fill_parent"

答案 2 :(得分:1)

您需要使ImageButton高度"wrap_content",并TextView"fill_parent"。您需要将它们都包装在布局父级中。这样,它们与相同的布局父级相关联。

答案 3 :(得分:0)

将您的元素放入LinearLayout,设置layout_width =&#34; match_parent&#34;并添加属性&#34; layout_weight&#34; =&#34; 1&#34;

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="sample text"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="sample text"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="sample text"/>
    </LinearLayout>