簡體   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