【问题标题】:Android RelativeLayout : Placing buttons side by side next to one anotherAndroid RelativeLayout:将按钮并排放置
【发布时间】:2016-08-04 12:04:40
【问题描述】:

我正在尝试使用 RelativeLayout 将三个按钮并排放置在一行中。

这是放置在带有按钮组的线性布局内的相对布局

ma​​in.xml

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#CC8FD8D8"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingBottom="20px" >
         </RelativeLayout> 
 </LinearLayout>      

这些是在布局中找到的一组按钮

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/snap"
            android:textColor="#FFFFFF"
            android:background="#00FFFFFF"
            android:id="@+id/shutterButton"
            android:text="SNAP"></Button>

                   <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/up"
            android:textColor="#FFFFFF"
            android:background="#00FFFFFF"
            android:id="@+id/Up"
            android:text="xxxx"></Button>

                   <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/ic_action_borrow"
            android:textColor="#FFFFFF"
            android:background="#00FFFFFF"
            android:id="@+id/xxxx"
            android:text="xxxxx"></Button>

                   <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/xxxx"
            android:textColor="#FFFFFF"
            android:background="#00FFFFFF"
            android:id="@+id/xxxx"
            android:text="xxxx"></Button>

                   <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableTop="@drawable/xxxx"
            android:textColor="#FFFFFF"
            android:background="#00FFFFFF"
            android:id="@+id/xxxx"
            android:text="xxxx"></Button>

输出 请问如何将按钮一个接一个地放置在 3 个按钮中的一排。 请协助!

【问题讨论】:

  • 这种类型使用LinearLayouts
  • 我有 LinearLayouts 并且都垂直对齐
  • LinearLayoutorientation="horizontal" 会非常简单。虽然如果你仍然想使用 RelativeLayout 使用 android:layout_toEndOf 和/或 android:layout_toRightOf 属性。
  • 竖直放置但没有停在第三个按钮
  • “没有停在”是什么意思?

标签: android android-layout


【解决方案1】:

试试这个:

XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CC8FD8D8"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="20px">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="SNAP"
            android:textColor="#FFFFFF"></Button>
        <!--android:drawableTop="@drawable/snap"-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="xxxx"
            android:textColor="#FFFFFF"></Button>

        <Button
            android:id="@+id/xxxx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="xxxxx"
            android:textColor="#FFFFFF"></Button>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CC8FD8D8"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="20px">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="xxxx"
            android:textColor="#FFFFFF"></Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="xxxx"
            android:textColor="#FFFFFF"></Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:text="xxxx"
            android:textColor="#FFFFFF"
            android:visibility="invisible"></Button>
    </LinearLayout>
</LinearLayout>

输出将是:

【讨论】:

  • @JnG 那是完美的解决方案。
  • @JnG 如果您认为答案对您有帮助,请投票;)
【解决方案2】:

以下代码水平并排创建 4 个按钮

   <LinearLayout
    android:layout_marginTop="1dp"
    android:layout_marginBottom="1dp"
    android:id="@+id/lay"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button1"
        android:text="Button1"
        android:layout_weight="1"
        android:padding="5dp" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button2"
        android:text="Button2"
        android:layout_weight="1"
        android:padding="5dp" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button3"
        android:text="Button3"
        android:layout_weight="1"
        android:padding="5dp" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button4"
        android:text="Button4"
        android:layout_weight="1"
        android:padding="5dp" />

</LinearLayout>

【讨论】:

  • 我赞成这个答案
【解决方案3】:

您应该为这种水平按钮进行线性布局,并且不要忘记将 LinearLayout 内部的方向设置为水平。 喜欢

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#CC8FD8D8"
            android:gravity="center"
            android:orientation="horizontal" 
            android:paddingBottom="20px" >

然后在这个线性布局中添加按钮,所有的按钮都会并排出现。

【讨论】:

    【解决方案4】:

    试试LinearLayoutandroid:orientation="horizontal"。在下面检查 -

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#CC8FD8D8"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingBottom="20px">
    
        <Button
            android:id="@+id/shutterButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:drawableTop="@drawable/snap"
            android:text="SNAP"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/Up"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:drawableTop="@drawable/up"
            android:text="xxxx"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/xxxx"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:drawableTop="@drawable/ic_action_borrow"
            android:text="xxxxx"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/xxxx"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:drawableTop="@drawable/xxxx"
            android:text="xxxx"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/xxxx"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FFFFFF"
            android:drawableTop="@drawable/xxxx"
            android:text="xxxx"
            android:textColor="#FFFFFF" />
    
    </LinearLayout>
    

    【讨论】:

    • 再次检查问题。
    • 请问如何将 3 个按钮中的按钮一个接一个地放置在一行中。请协助!
    • 你能给我你想要的图片介绍吗?因为我听不懂你想说什么。
    • @JnG 想要回答。我已经回答过了。只需检查下面的图片。
    • 是的......现在你得到了他的问题。回答接受。只需检查一下。
    【解决方案5】:

    试试这个

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#CC8FD8D8"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="20px" >
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/snap"
                android:textColor="#FFFFFF"
                android:background="#00FFFFFF"
                android:id="@+id/shutterButton"
                android:text="SNAP"></Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/up"
                android:textColor="#FFFFFF"
                android:background="#00FFFFFF"
                android:id="@+id/Up"
                android:text="xxxx"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/xxxxx"
                android:layout_toStartOf="@+id/xxxxx"
                android:layout_marginRight="27dp"
                android:layout_marginEnd="27dp"></Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/ic_action_borrow"
                android:textColor="#FFFFFF"
                android:background="#00FFFFFF"
                android:id="@+id/xxxxx"
                android:text="xxxxx"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"></Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/xxxx"
                android:textColor="#FFFFFF"
                android:background="#00FFFFFF"
                android:id="@+id/xxxxxx"
                android:text="xxxx"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/xxxx"
                android:layout_toStartOf="@+id/xxxx"
                android:layout_marginRight="50dp"
                android:layout_marginEnd="50dp"></Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableTop="@drawable/xxxx"
                android:textColor="#FFFFFF"
                android:background="#00FFFFFF"
                android:id="@+id/xxxx"
                android:text="xxxx"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"></Button>
    
        </RelativeLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2011-09-28
      • 1970-01-01
      • 2021-02-13
      相关资源
      最近更新 更多