在RelativeLayout中对齐组件

4

|--按钮1--|

|-按钮2-|

使用RelativeLayout,如何将按钮2的宽度与按钮1匹配对齐。

3个回答

5
<Button
        android:id="@+id/A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dip"
        android:textSize="14sp"
        android:text="--Button1--" />

<Button
        android:id="@+id/B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/A"
        android:gravity="center"
        android:text="Button2"
        android:layout_alignLeft="@id/A"
        android:layout_alignRight="@id/A"
        android:textSize="14sp" />

在您的RelativeLayout中尝试上述内容。


我之前尝试过这个,但没有成功。这就是为什么我发了这个问题。无论如何,感谢你的尝试,@Varun。 - Ragunath Jawahar
抱歉@Varun,我将我的按钮的layout_width设置为“fill_parent”,这就是为什么我没有得到我想要的结果。你是正确的。谢谢。 - Ragunath Jawahar

1
你要找的应该是layout_alignRight。这将把你的View的右边缘与给定锚点的右边缘对齐。

-1
如果您想让按钮2依赖于按钮1,那么据我所知是没有办法的。但是,如果您正在设计一个动态布局,您可以通过编程实现这一点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接