【问题标题】:View pager with tabs layout is out of screen bounds带有选项卡布局的视图寻呼机超出屏幕范围
【发布时间】:2020-01-14 11:38:46
【问题描述】:

我正在使用选项卡布局并在视图分页器下方显示所选片段的内容。

在布局的顶部是选项卡布局,在视图分页器的下方。问题是:视图分页器占据了全屏的高度,因此(因为它被限制在导航选项卡上),它的高度像这样离开屏幕:

我的 xml:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".activities.MeetActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/navigationTabs"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/navigation_shadow"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicator="@null"
        app:tabMinWidth="@dimen/navigation_height"
        app:tabRippleColor="@null" />

<!--    // the fragment will be displayed here:-->
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/navigationTabs"/>

有没有办法将视图寻呼机限制在导航选项卡的顶部并强制其高度保持在屏幕范围内?这可能看起来微不足道,但它会导致片段中的小部件出现在屏幕之外

【问题讨论】:

    标签: android xml android-viewpager


    【解决方案1】:

    视图分页器采用全屏高度

    ViewPageronMeasure函数中,wrap_content被计算为容器大小(在本例中为ConstaraintLayout)。

    试试这个。

    <androidx.viewpager.widget.ViewPager
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="0dp" 
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/navigationTabs" />
    

    【讨论】:

      【解决方案2】:

      在这里,您的视图寻呼机占据了全屏的高度。请用 下面的代码显示您的标签下方的视图寻呼机:

      <androidx.viewpager.widget.ViewPager
              android:id="@+id/fragmentContainer"
              android:layout_width="match_parent"
              android:layout_height="0dp" 
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintTop_toBottomOf="@+id/navigationTabs" />
      

      【讨论】: