【问题标题】:One button on left side other on the right一个按钮在左边另一个在右边
【发布时间】:2024-04-29 12:10:04
【问题描述】:

我知道有这样的问题,但大多数时候他们的问题似乎是他们没有将布局对齐为填充父级。好吧,我这样做了,我不确定为什么我的按钮仍然无法做到。可能是因为我使用的所有布局?

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/face">

  <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
  <ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="@+id/eyes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/eyes1" />
        <LinearLayout android:layout_marginTop="50dp"  android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal">
            <Button android:id="@+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" />
            <Button android:id="@+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right"></Button>
        </LinearLayout>
  </RelativeLayout>

</LinearLayout>

它可能我也错过了一些简单的东西。感谢您的帮助。

【问题讨论】:

    标签: android xml button


    【解决方案1】:

    试试这个

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/face">
    
      <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
      <ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="@+id/eyes" android:background="@drawable/eyes1" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
            <RelativeLayout android:layout_marginTop="50dp"  android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal">
                <Button android:id="@+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/>
                <Button android:id="@+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right" android:layout_alignParentRight="true"></Button>
            </RelativeLayout>
      </RelativeLayout>
    
    </LinearLayout>
    

    我只是简单地将线性布局更改为相对布局并将属性设置为一个按钮以从左到右另一个按钮。

    【讨论】:

      【解决方案2】:
      <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/face">
      
        <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:gravity="center">
        <ImageView android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:id="@+id/eyes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/eyes1" />
                  <Button android:id="@+id/eyesback" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/>
                  <Button android:id="@+id/eyesforward" android:layout_height="50dp" android:layout_width="50dp" android:layout_gravity="right" android:layout_alignParentRight="true"></Button>
        </RelativeLayout>
      
      </LinearLayout>
      

      试试看

      【讨论】:

      • 谢谢,但它仍然没有移动它们。
      • 你想要一个按钮在父母的左边和一个在右边,那么它工作正常
      【解决方案3】:

      【讨论】: