简体   繁体   中英

Android Tab Fragment Formatting Bug

*EDIT: I've included the code for my sytles.xml file at the bottom of this post, since the bug might be in there.

I'm developing a basic Android app that uses a tab interface, where each tab contains a corresponding fragment.

在此处输入图片说明

I'm encountering a formatting bug when I switch to the first tab. As you can see in the next image, all the items are pushed down. This only occurs in that first tab, the one that is initially launched by the app; the second and third are fine.

在此处输入图片说明

What is causing this bug?

I've pinpointed the bug to API 21 (Lollipop) and above. On Kitkat (API 19), the content stays in place as it's supposed to on that first tab.

I've looked through all the XML files for padding or margin irregularities, but couldn't find anything.

Also, it doesn't matter what fragment I put in the first tab's slot; the bug occurs regardless.

Because the bug is absent on Kitkat, I'm tempted to attribute it to the Android OS itself, but any clarification would be helpful.

Here is the high-level code I have for the tabs, which might be helpful.

public class PagerAdapter extends FragmentStatePagerAdapter {

    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs){
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Tab2 tab1 = new Tab2();
                return tab1;
            case 1:
                Tab1 tab2 = new Tab1();
                return tab2;
            case 2:
                Tab3 tab3 = new Tab3();
                return tab3;
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

My styles.xml file:

<resources>>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

It looks like your layout is allowed to be behind the navigation bar, which is possible since Android 5.0 Lollipop, hence it's no issue on KitKat (or older versions for that matter).

Make sure you don't have set windowDrawsSystemBarBackgrounds to true anywhere. You may have to set it explicitly to false in your app's theme:

<style name="YourAppTheme">
    ...
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

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