简体   繁体   中英

Fragments are not showing inside tablayout

I'm populating TabLayout with fragments without ViewPager . But when selected a tab my fragments are not displaying inside tab. If I'm making it below toolbar its text is diplayed below toolbar and at the top of tabs, but it never shows inside tab. Here is the 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" />

And the MainActivity is here.

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();

Each Fragment has a single TextView .

Firstly, as you didn't provide your full xml, I think the Toolbar , TabLayout and the FrameLayout are inside a RelativeLayout .

Now as far as I can understand you could load the Fragment s correctly, but they are not showing as they are hidden under some other layout. So to get rid of your problem you need to make the layout design like this.

<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/>

Just get rid of FrameLayout and use a RelativeLayout as your fragment container and put it under your TabLayout to see if its working now.

根布局应为线性布局,不了解为什么必须为线性布局,但其方式为线性布局

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM