【问题标题】:How to align two buttons next to each other?如何对齐两个相邻的按钮?
【发布时间】:2020-03-17 13:58:23
【问题描述】:

我正在尝试将两个浮动按钮并排放置,我尝试使用设计视图移动它,但我无法移动它返回到同一个位置的按钮。

我仍然对 ConstraintLayout、RelativeLayout 和 LinearLayout 术语感到困惑。

根据我的阅读,LinearLayout 是用于水平视图的,所以我尝试更改布局的宽度和高度以包装内容

这是我目前的结果

activity_main

<androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="9dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="9dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true">

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

                <EditText
                    android:id="@+id/resultText"
                    android:layout_width="363dp"
                    android:layout_height="wrap_content"
                    android:autoLink="all"
                    android:autofillHints="text edit"
                    android:background="@drawable/bg_edit"
                    android:gravity="top"/>

                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:id="@+id/saveBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"/>

                <com.google.android.material.floatingactionbutton.FloatingActionButton
                    android:id="@+id/pdfBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:orientation="vertical"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    由于您的按钮位于具有 Vertical 方向的 LinearLayout 中,因此如果您将按钮放在容器中然后将它们彼此相邻对齐,那么您的按钮会相互堆叠。

    出于解释的目的,我使用了Constraint Layout 设置了约束,例如将pdf Button 旁边的约束Save Buttonpdf Button 放在父级的末尾。

    试试下面的代码

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="9dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="9dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:cardUseCompatPadding="true">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                    <EditText
                        android:id="@+id/resultText"
                        android:layout_width="363dp"
                        android:layout_height="wrap_content"
                        android:autoLink="all"
                        android:autofillHints="text edit"
                        android:background="@drawable/bg_edit"
                        android:gravity="top" />
    
                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <com.google.android.material.floatingactionbutton.FloatingActionButton
                            android:id="@+id/saveBtn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
    
                            android:layout_gravity="end"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toStartOf="@+id/pdfBtn"
                            app:layout_constraintHorizontal_bias="1.0"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
    
                        <com.google.android.material.floatingactionbutton.FloatingActionButton
                            android:id="@+id/pdfBtn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:orientation="vertical"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
                    </androidx.constraintlayout.widget.ConstraintLayout>
    
    
                </LinearLayout>
            </androidx.cardview.widget.CardView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-23
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多