【问题标题】:ConstraintLayout not filling whole screenConstraintLayout 没有填满整个屏幕
【发布时间】:2021-10-07 19:33:48
【问题描述】:

我在NestedScrollView 中有ConstraintLayout。 如果我不使用NestedScrollView 包裹ConstraintLayout,那么ConstraintLayout 会占据整个屏幕区域,但是一旦我用NestedScrollView 包裹它,ConstraintLayout 就会包裹它的视图并且只占用包裹的空间。

但是,我需要 ConstraintLayout 与其父 NestedScrollView 或至少在屏幕高度保持一致。

对我来说,我在ViewPager2 中添加了片段。当ConstraintLayout 包装其内容视图时,我无法在运行时看到ViewPager2 中的片段。

如果我删除 NestedScrollView,一切正常,我可以正确查看所有片段。

这是我的布局代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:text="Sample Text"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        android:layout_marginTop="32dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView">
        
        <com.google.android.material.tabs.TabItem
            android:id="@+id/first"
            android:text="First"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <com.google.android.material.tabs.TabItem
            android:id="@+id/second"
            android:text="Second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <com.google.android.material.tabs.TabItem
            android:id="@+id/third"
            android:text="Third"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </com.google.android.material.tabs.TabLayout>
    
    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabLayout"
        app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

我有一些图片。

任何帮助将不胜感激。 谢谢

【问题讨论】:

标签: android android-constraintlayout android-nestedscrollview


【解决方案1】:

android:fillViewport="true" 帮助扩展了片段,这意味着我能够在运行时看到片段。但是,碎片有不同的高度。因此,另一个问题是 ViewPager 为所有片段分配了相同的高度。

最后,在每个片段的onResume 方法上,我需要调用requestLayout()。 从而解决了片段高度不同的问题。

override fun onResume() {
   super.onResume()
   binding.root.requestLayout()
}

答案链接:Answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多