繁体   English   中英

如何让两个按钮并排粘在一起?

[英]How to make 2 buttons stick together side by side?

好的,所以我在具有 2 个按钮的 RelativeLayout 中有此代码:

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

    <Button
        android:id="@+id/xoay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="500dp"
        android:layout_marginLeft="160dp"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="Xoay" />
    <Button
        android:id="@+id/chon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="400dp"
        android:layout_marginLeft="160dp"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="Chọn" />

</RelativeLayout>

问题是,用户只能同时看到 1 个按钮,因为他们在同一个地方。 我希望这两个按钮紧挨在一起。 在RelativeLayout 有没有办法做到这一点? 谢谢

我用线性布局包裹按钮,以便将方向设置为水平,这可以并排对齐布局内的元素

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_centerInParent="true">

        <Button
            android:id="@+id/xoay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Xoay" />

        <Button
            android:id="@+id/chon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chọn" />

    </LinearLayout>




</RelativeLayout>

通过使用水平线性布局,您可以轻松实现这一点。,

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

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

        <Button
            android:id="@+id/chon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:layout_marginLeft="110dp"
            android:text="Chọn" />

        <Button
            android:id="@+id/xoay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Xoay" />
    </LinearLayout>

</RelativeLayout>

如果您要构建复杂的布局,我建议您可以尝试使用约束布局

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM