簡體   English   中英

如何在 ScrollView 內的 ConstraintLayout 中將視圖居中?

[英]How do I center a View in a ConstraintLayout inside a ScrollView?

我需要在 ConstraintLayout 中將 ImageView 居中。 問題是,由於它是一個ScrollView,ConstraintLayout 比屏幕高,也就是說,我無法將ImageView 的底部限制到。 我試圖將 ImageView 和整個 ScrollView 放在另一個 ConstraintLayout 中,但這會與動畫混淆(我將其轉換為 MotionLayout)。 有沒有辦法設置android:gravity="center"或以某種方式參考屏幕高度(據我所知,不可能)? 我是初學者,所以請 go 放輕松。 謝謝你。

<?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 個小部件之間設置一個視圖。 這里是ImageViewwidget.MotionLayout之間的LinearLayoutRelativeLayout

<?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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM