如何对齐两个按钮彼此相邻?
How to align two buttons next to each other?
我试图将两个浮动按钮并排放置,我尝试在设计视图中移动它,但我无法移动按钮,它会回到同一位置。
我仍然对 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>
由于您的按钮位于具有 Vertical
方向的 LinearLayout 中,因此如果您将按钮放在一个容器中然后将它们彼此相邻对齐,那么它就会工作。
出于解释的目的,我使用了 Constraint Layout
约束设置,例如 pdf Button
旁边的约束 Save 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>
我试图将两个浮动按钮并排放置,我尝试在设计视图中移动它,但我无法移动按钮,它会回到同一位置。
我仍然对 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>
由于您的按钮位于具有 Vertical
方向的 LinearLayout 中,因此如果您将按钮放在一个容器中然后将它们彼此相邻对齐,那么它就会工作。
出于解释的目的,我使用了 Constraint Layout
约束设置,例如 pdf Button
旁边的约束 Save 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>