将ConstraintLayout垂直居中,并在ScrollView中使用可扩展的textViews(滚动/居中问题)

时间:2020-01-22 08:53:40

标签: android textview scrollview android-constraintlayout centering

我在ConstraintLayout内创建了具有5个可扩展文本视图的简单布局。它必须是可滚动的(因为会有很多内容),所以所有这些都在ScrollView内部。我想使所有这些textView垂直居中(看起来像第一张图片),所以我在自己的Constrait中使用了android:layout_gravity="center"。首先,我注意到,当我在ScrollView上使用android:fillViewport="true"时,center无效。否则,当我使用android:fillViewport="false"中心工作时,但TextView包含很多内容时,它将无法正确滚动。在第二张图片上,您可以看到我的1-3 textview只是隐藏了(屏幕最大滚动到顶部),在下一张图片上,我们可以看到scrollView不适合内容,并且有很多空白空间(该空间应该在顶部,我缺少的1-3 textViews在哪里。为什么 ?如何解决这个问题?

这里有一些代码

<LinearLayout
    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:background="@color/colorPrimary"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/NavigateTopInfo"
        android:layout_marginTop="@dimen/_5sdp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/MapButton"
            android:layout_width="@dimen/_30sdp"
            android:layout_height="@dimen/_30sdp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="@dimen/_5sdp"
            android:background="@drawable/ic_map"
            android:backgroundTint="#ffffff"
            android:onClick="buttonClicked" />

        <ImageButton
            android:id="@+id/ExitButton"
            android:layout_width="@dimen/_30sdp"
            android:layout_height="@dimen/_30sdp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="@dimen/_5sdp"
            android:background="@drawable/ic_exit_to_app"
            android:backgroundTint="#ffffff"
            android:onClick="buttonClicked" />

    </RelativeLayout>


    <ScrollView
        android:id="@+id/ScrollTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        android:layout_marginTop="@dimen/_8sdp">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/myLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            tools:context=".InfoActivity">

            <TextView
                android:id="@+id/textview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_15sdp"
                android:layout_marginStart="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_15sdp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:background="@drawable/squaretextview"
                android:clickable="true"
                android:elevation="5dp"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:onClick="toggleView"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
                android:text="TextView1"
                android:textColor="@color/textColor"
                android:textSize="18sp" />


            <TextView
                android:id="@+id/textview1_idcontent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/lorepipsum"
                android:textSize="15sp"
                app:layout_constraintTop_toBottomOf="@+id/textview1" />



            <TextView
                android:id="@+id/textview2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_15sdp"
                android:layout_marginStart="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_15sdp"
                android:background="@drawable/squaretextview"
                android:clickable="true"
                android:elevation="5dp"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:onClick="toggleView"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
                android:text="TextView2"
                android:textColor="@color/textColor"
                android:textSize="18sp"
                app:layout_constraintTop_toBottomOf="@+id/textview1_idcontent" />




            <TextView
                android:id="@+id/textview2_idcontent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/lorepipsum"
                android:textSize="15sp"
                app:layout_constraintTop_toBottomOf="@+id/textview2" />



            <TextView
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_15sdp"
                android:layout_marginStart="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_15sdp"
                android:background="@drawable/squaretextview"
                android:clickable="true"
                android:elevation="5dp"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:onClick="toggleView"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
                android:text="TextView3"
                android:textColor="@color/textColor"
                android:textSize="18sp"
                app:layout_constraintTop_toBottomOf="@+id/textview2_idcontent" />


            <TextView
                android:id="@+id/textview3_idcontent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/lorepipsum"
                android:textSize="15sp"
                app:layout_constraintTop_toBottomOf="@+id/textview3_id" />



            <TextView
                android:id="@+id/textview4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_15sdp"
                android:layout_marginStart="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_15sdp"
                android:background="@drawable/squaretextview"
                android:clickable="true"
                android:elevation="5dp"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:onClick="toggleView"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
               android:text="TextView 4"
                android:textColor="@color/textColor"
                android:textSize="18sp"
                app:layout_constraintTop_toBottomOf="@+id/textview3_idcontent" />


            <TextView
                android:id="@+id/textview4_idcontent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/lorepipsum"
                android:textSize="15sp"
                app:layout_constraintTop_toBottomOf="@+id/textview4" />



            <TextView
                android:id="@+id/textview5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/_15sdp"
                android:layout_marginStart="@dimen/_15sdp"
                android:layout_marginTop="@dimen/_15sdp"
                android:background="@drawable/squaretextview"
                android:clickable="true"
                android:elevation="5dp"
                android:focusable="true"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="center"
                android:onClick="toggleView"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
                android:text="TextView5"
                android:textColor="@color/textColor"
                android:textSize="18sp"
                app:layout_constraintTop_toBottomOf="@+id/textview4_idcontent" />


            <TextView
                android:id="@+id/textview5_idcontent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="@string/lorepipsum"
                android:textSize="15sp"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

        </android.support.constraint.ConstraintLayout>
    </ScrollView>

</LinearLayout>

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

请勿使用android:layout_gravity="center"ConstraintLayout。它负责第二第三问题。要使视图垂直居中,请将ScrollView放在RelativeLayout内,并将centerInParent设置为true。除此之外,要使视图在ConstraintLayout中水平居中,请使用约束到parent并保持 width wrap_content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/NavigateTopInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--Header content -->

    </RelativeLayout>

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:text="TextView1" />

                <TextView
                    android:id="@+id/textview1_idcontent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView1 Content"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textview1" />

                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:text="TextView2"
                    app:layout_constraintTop_toBottomOf="@+id/textview1_idcontent" />

                <TextView
                    android:id="@+id/textview2_idcontent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextView1 Content"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textview2" />

                <!--Other content -->

            </androidx.constraintlayout.widget.ConstraintLayout>
        </ScrollView>

    </RelativeLayout>

</LinearLayout>