【问题标题】:How do I center a View in a ConstraintLayout inside a ScrollView?如何在 ScrollView 内的 ConstraintLayout 中将视图居中?
【发布时间】:2021-07-28 03:28:58
【问题描述】:

我需要在 ConstraintLayout 中将 ImageView 居中。问题是,因为它是一个 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" />
.
.
.

【问题讨论】:

    标签: android android-layout android-xml android-constraintlayout android-scrollview


    【解决方案1】:

    您需要在 2 个小部件之间设置一个视图。这里是LinearLayoutRelativeLayoutImageViewwidget.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

    【讨论】:

    • 我的 ScrollView 已经有了。您能否详细说明它应该如何/为什么起作用?
    • 看到很晚。我正在努力,给我一点时间来准备我的烤面包机。
    • 完成。干杯:)
    • 那并没有真正做任何事情,我在对齐 LinearLayout 时遇到了同样的问题;由于 ImageView 不再在 MotinLayout 中,我无法对其进行动画处理。
    • 这可以帮助您定制:stackoverflow.com/questions/60838660/…
    猜你喜欢
    • 2020-05-08
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多