Android ConstraintLayout:将一个元素设置在另一个元素的中心
Android ConstraintLayout: set one element in the center of another one
我正在使用 ConstraintLayout 来创建 activity 视图
有一段代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".presentation.screens.registration.RegistrationActivity">
<LinearLayout>...</LinearLayout>
<Button
android:id="@+id/login_next_btn"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/button_green_disabled"
android:layout_width="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:text="@string/next"
android:textColor="@color/font_white"
android:textSize="16sp"
android:layout_height="48dp"
android:enabled="false"
android:onClick="onNextClick"/>
<ImageView
android:id="@+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/login_next_btn"
android:src="@drawable/ic_loader"/>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是:如何将ImageView(android:id="@+id/loader_image")设置在Button(@+id/login_next_btn)的中间?
试试这个:
<ImageView
android:id="@+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/login_next_btn"
app:layout_constraintRight_toRightOf="@id/login_next_btn"
app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
android:src="@drawable/ic_loader"/>
如果您必须将它设置在中心,只需更改图像的约束:
app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
app:layout_constraintStart_toStartOf="@id/login_next_btn"
app:layout_constraintEnd_toEndOf="@id/login_next_btn"
我正在使用 ConstraintLayout 来创建 activity 视图
有一段代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".presentation.screens.registration.RegistrationActivity">
<LinearLayout>...</LinearLayout>
<Button
android:id="@+id/login_next_btn"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@drawable/button_green_disabled"
android:layout_width="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:text="@string/next"
android:textColor="@color/font_white"
android:textSize="16sp"
android:layout_height="48dp"
android:enabled="false"
android:onClick="onNextClick"/>
<ImageView
android:id="@+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/login_next_btn"
android:src="@drawable/ic_loader"/>
</androidx.constraintlayout.widget.ConstraintLayout>
问题是:如何将ImageView(android:id="@+id/loader_image")设置在Button(@+id/login_next_btn)的中间?
试试这个:
<ImageView
android:id="@+id/loader_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/login_next_btn"
app:layout_constraintRight_toRightOf="@id/login_next_btn"
app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
android:src="@drawable/ic_loader"/>
如果您必须将它设置在中心,只需更改图像的约束:
app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
app:layout_constraintTop_toTopOf="@id/login_next_btn"
app:layout_constraintStart_toStartOf="@id/login_next_btn"
app:layout_constraintEnd_toEndOf="@id/login_next_btn"