如何解决如何在 ScrollView 内的 ConstraintLayout 中将视图居中?
我需要将 ImageView 放在 ConstraintLayout 中。问题是,由于它是 ScrollView,ConstraintLayout 比屏幕高,也就是说,我无法将 ImageView 的底部约束到。我试图将 ImageView 和整个 ScrollView 放在另一个 ConstraintLayout 中,但这会混淆动画(我将其转换为 MotionLayout)。有没有办法设置 android:gravity="center"
或以某种方式引用屏幕高度(据我所知,不可能)?我是初学者,所以请放轻松。谢谢。
<?xml version="1.0" encoding="utf-8"?>
<!-- This ScrollView is taller than the screen. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:scrollbars="none">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/cl_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_color_on_primary_dark"
app:layoutDescription="@xml/activity_permissions_scene"
tools:context=".Permissions">
<!-- How do I center this ImageView? -->
<ImageView
android:id="@+id/perm_logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:contentDescription="@string/perm_iv_camera_content_description"
android:src="@drawable/ic_antibody_splashscreen_face"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
.
.
.
解决方法
您需要在 2 个小部件之间设置一个视图。这里是 LinearLayout
和 RelativeLayout
之间的 ImageView
或 widget.MotionLayout
:
<?xml version="1.0" encoding="utf-8"?>
<!-- This ScrollView is taller than the screen. -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:scrollbars="none">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/cl_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_color_on_primary_dark"
app:layoutDescription="@xml/activity_permissions_scene"
tools:context=".Permissions">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- ^Set A LinearLayout or RelativeLayout between Widgets^ -->
<ImageView
android:id="@+id/perm_logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:contentDescription="@string/perm_iv_camera_content_description"
android:src="@drawable/ic_antibody_splashscreen_face"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
.
.
.
现在您可以将 android:layout_gravity="center"
设置为您的 ImageView
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。