【问题标题】:Fragments are not showing inside tablayout片段未在 tablayout 中显示
【发布时间】:2017-06-24 19:44:18
【问题描述】:

我正在用没有ViewPager 的片段填充TabLayout。 But when selected a tab my fragments are not displaying inside tab.如果我将其置于工具栏下方,则其文本将显示在工具栏下方和选项卡顶部,但它永远不会显示在选项卡内。这是 XML。

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/toolbarColor"
    android:elevation="3dp"
    />

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:layout_below="@+id/my_toolbar" />

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MainActivity 就在这里。

switch(tab.getPosition()){
    case 0:
        Log.i("MainActivity ", "one ");
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.frame, new TodayFragment());

        break;

    case 1:
        FragmentManager fm1 = getSupportFragmentManager();
        ft = fm1.beginTransaction();
        Log.i("MainActivity ", "two");
        ft.replace(R.id.frame, new SettingsFragment());
}

ft.addToBackStack(null);
ft.commit();

每个Fragment 都有一个TextView

【问题讨论】:

  • 请给minimal reproducible example。请注意,这并不意味着发布您的整个应用程序。
  • 能否请您提供App的截图,比如它是如何显示您的屏幕的。
  • Its just two blank tabs and when tab is selected nothing happens it remains blank

标签: java android android-fragments android-tablayout


【解决方案1】:

首先,由于您没有提供完整的 xml,我认为 ToolbarTabLayoutFrameLayoutRelativeLayout 内。

现在据我所知,您可以正确加载Fragments,但它们没有显示,因为它们隐藏在其他布局下。所以要摆脱你的问题,你需要像这样进行布局设计。

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/toolbarColor"
        android:elevation="3dp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_below="@+id/my_toolbar" />

    <RelativeLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs"/>
<RelativeLayout/>

只需摆脱FrameLayout 并使用RelativeLayout 作为您的片段容器并将其放在您的TabLayout 下,看看它现在是否可以工作。

【讨论】:

  • 片段可以在相对布局内动态变化,是的,根布局是相对布局
  • 为什么不呢。你也可以有一个LinearLayout 作为你的片段持有者。它实际上取决于你。但正如我所建议的,越简单越好。
  • 是的,请看我的其他问题
  • 你还有什么问题?
【解决方案2】:

根布局应该是线性布局不明白为什么它必须是线性布局但它是这样的

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2016-11-15
    • 2021-06-23
    • 2020-12-11
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多