【问题标题】:How to Align 3 buttons in a line, android?如何在一行中对齐 3 个按钮,android?
【发布时间】:2014-01-02 02:08:07
【问题描述】:

我在父级中有线性垂直布局。 然后是底部的水平线性布局,包括 3 个按钮

我想要底部活动最左侧的第一个按钮 中间的第二个按钮 和最右侧的第三个按钮

这里是xml

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

   <ImageView
    android:id="@+id/viewImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:contentDescription="@null"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" >

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

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

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_gravity="fill_vertical"
        android:text="Button" />
</LinearLayout>

【问题讨论】:

    标签: android button alignment android-linearlayout


    【解决方案1】:

    你也可以用约束布局来做到这一点

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button3"
        app:layout_constraintTop_toTopOf="@+id/button3" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button" />
    
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="102dp"
        android:text="Button"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    ss:-

    【讨论】:

      【解决方案2】:

      是的,您可以通过申请layout_weight=1

      <?xml version="1.0" encoding="utf-8"?>
      <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"
              android:layout_alignParentBottom="true">
              <Button
                  android:id="@+id/button1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="left " />
              <Button
                  android:id="@+id/button1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Center" />
              <Button
                  android:id="@+id/button1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Right" />
          </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        设置方向有水平

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
             >
        
            <Button
                android:id="@+id/button1"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        
            <Button
                android:id="@+id/button2"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        
            <Button
                android:id="@+id/button3"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:layout_gravity="fill_vertical"
                android:text="Button" />
        </LinearLayout>
        

        并为按钮使用权重

        捕捉

        【讨论】:

          【解决方案4】:

          对于那些类型的对齐方式(一个按钮在父布局的最左边,另一个在中间,另一个在父布局的最右边)RelativeLayout 将比LinearLayout 更方便。因为,如果你使用@987654323 @ 作为您的按钮的父布局,您可以使用 android:layout_alignParentLeft="true" 作为一个 button,您希望在 left 中对齐,android:layout_centerInParent="true"center 中对齐和android:layout_alignParentRight="true"侧角对齐按钮。

          试试这个..

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >
          
             <ImageView
              android:id="@+id/viewImageView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:contentDescription="@null"
              android:src="@drawable/ic_launcher" />
          
          <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom" >
          
              <Button
                  android:id="@+id/button1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:text="Button" />
          
              <Button
                  android:id="@+id/button2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:text="Button" />
          
              <Button
                  android:id="@+id/button3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:text="Button" />
          </RelativeLayout>
          
          </LinearLayout>
          

          【讨论】:

          • @Wishy 如果我的回答对您有所帮助,请接受。并通过我的编辑获得更多解释。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-21
          • 2016-09-23
          • 1970-01-01
          • 2011-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多