约束不能使电话视图居中

时间:2020-04-28 10:03:59

标签: android android-layout android-constraintlayout

我有一个奇怪的错误。我真的不知道为什么。

因此,在编辑器上,我的约束可以按我的意愿工作。但是在电话上却没有。

图片(我没有足够的声誉来显示图片。.):

android studio

phone problem

代码:

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".fragments.home.HomeFragment">

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="some text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="some text"
                app:layout_constraintBottom_toTopOf="@+id/textView2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</layout>

提前谢谢!

2 个答案:

答案 0 :(得分:0)

您是否尝试过将ConstraintLayout的高度设置为“ match_parent”? 似乎您将其限制为TextViews的组合高度。

此外,您不应以这种方式设置BottomToTop和TopToBottom,一个应该取决于父项。您可以通过在编辑器中选择两个视图,然后右键单击选择的Chain Vertically来轻松实现。

答案 1 :(得分:0)

没关系,只是发现了问题。

宽度和高度都必须在match_parent中的NavHostFragmentactivity_main.xml

<!-- navigation fragment -->
<androidx.fragment.app.FragmentContainerView
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:defaultNavHost="true"
    app:navGraph="@navigation/navigation"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
相关问题