如何将两个按钮彼此对齐?

时间:2019-11-21 15:22:34

标签: android android-layout android-button

我试图将两个浮动按钮彼此相邻放置,我尝试在设计视图中移动它,但是我无法将其移回同一位置。

我对ConstraintLayout,Relativelayout和LinearLayout术语仍然感到困惑。

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

这是我当前的结果

result

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>

1 个答案:

答案 0 :(得分:0)

由于您的按钮位于Vertical方向的LinearLayout中,因为如果将按钮放置在容器中,然后将它们并排对齐,则它们会彼此堆叠,那么它将起作用。

出于解释目的,我使用了Constraint Layout约束,设置为约束Save Button旁边的约束pdf Button和父级末尾的pdf 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>
相关问题