有没有"match_sibling"这样的东西?(而不是"匹配父母")

use*_*281 2 android android-layout

有没有办法快速轻松地让一个视图匹配其兄弟姐妹的高度或宽度?让我说我有这个按钮,它有一个兄弟视图元素,在这种情况下我不关心宽度,但我需要这个按钮的高度来匹配他的兄弟姐妹的

    <Button
        android:layout_width="wrap_content"
        android:layout_height="???"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>
Run Code Online (Sandbox Code Playgroud)

stk*_*ent 6

你可以使用RelativeLayoutlayout_alignLeftlayout_alignRightXML属性相匹配的宽度(类似性质layout_alignToplayout_alignBottom将工作匹配高度).这只适用于您希望视图对齐的情况 - 否则,您将不得不使用维度资源.

以下是对齐按钮和匹配高度的示例:

<RelativeLayout
    ...>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_toRightOf="@id/button1"
        android:layout_alignTop="@id/button1"
        android:layout_alignBottom="@id/button1"
        android:text="@string/muh_other_button"
        android:onClick="do_other_stuff"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

以下是维度资源的示例:

<SomeViewGroup
    ...>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/muh_button"
        android:onClick="do_stuff"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/muh_other_button"
        android:onClick="do_other_stuff"/>

</SomeViewGroup>
Run Code Online (Sandbox Code Playgroud)

维度button_height定义的位置dimens.xml.