如何在 android studio 中将居中按钮置于 2 个线性布局下方
How to put centered button below 2 linear layouts in android studio
我的目标是让 2 列带有一个编辑文本和几个按钮,但我需要在屏幕底部有一个按钮来确认这些值。我无法将它放到屏幕底部并使其居中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/We"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/They"/>
</LinearLayout>
这个XML布局会帮助你
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/We"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/They"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
我认为使用 RelativeLayout
android:layout_alignParentBottom="true"
看看这个:
<?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"
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="Confirm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<LinearLayout
android:id="@+id/col1"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/col2"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="We"
android:inputType="numberSigned" />
</LinearLayout>
<LinearLayout
android:id="@+id/col2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintLeft_toRightOf="@id/col1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="They"
android:inputType="numberSigned" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
它将生成以下布局:
我的目标是让 2 列带有一个编辑文本和几个按钮,但我需要在屏幕底部有一个按钮来确认这些值。我无法将它放到屏幕底部并使其居中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/We"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/They"/>
</LinearLayout>
这个XML布局会帮助你
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/We"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:hint="@string/They"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
我认为使用 RelativeLayout
android:layout_alignParentBottom="true"
看看这个:
<?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"
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="Confirm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<LinearLayout
android:id="@+id/col1"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/col2"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/etWe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="We"
android:inputType="numberSigned" />
</LinearLayout>
<LinearLayout
android:id="@+id/col2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintLeft_toRightOf="@id/col1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/etThey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="They"
android:inputType="numberSigned" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
它将生成以下布局: