【问题标题】:Tab layout content not showing inside a Fragment layout选项卡布局内容未显示在片段布局内
【发布时间】:2018-09-04 11:33:21
【问题描述】:

我有一个BottomNavigationView,它有三个标签。一个BottomNavigationView 包含TabLayout。我的问题是为什么没有显示 TabLayout 内容。仅显示选项卡标题。

这是一个片段 XML。

 <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt_profile_about_me"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:textAlignment="viewStart"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txt_gathring_count" />



    <android.support.design.widget.TabLayout
        android:id="@+id/profile_tabs"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txt_profile_about_me"
        app:layout_constraintVertical_bias="1.0"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/profile_viewpager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/white"/>

</android.support.constraint.ConstraintLayout>

我的代码如下。

    ViewPager viewPager = (ViewPager) view.findViewById(R.id.profile_viewpager);
        ProfileTabAdapter adapter = new ProfileTabAdapter(getChildFragmentManager());

        // Set the adapter onto the view pager
        viewPager.setAdapter(adapter);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.profile_tabs);
        tabLayout.setupWithViewPager(viewPager);

标签内容未显示。任何建议。

【问题讨论】:

  • 也许您的viewPager 的高度和宽度为零?
  • 给viewPager高度=match_parent

标签: android android-layout android-fragments android-tablayout


【解决方案1】:

正确限制您的 viewPager。

试试这个:

    <android.support.v4.view.ViewPager
    android:id="@+id/profile_viewpager"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/profile_tabs"
    app:layout_constraintBottom_toBottomOf="parent"/>

【讨论】:

  • 快乐编码 ;)
【解决方案2】:

尝试添加:

app:layout_constraintTop_toBottomOf="@+id/profile_tabs"

ViewPager

【讨论】:

  • 即使宽度是 0dp,它也限制在父级的开始和结束,这意味着它相当于 match_parent
猜你喜欢
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多