TabHost:切换选项卡后,第一个加载的选项卡保留在后台 [英] TabHost: first loaded tab stays in background after switching tabs

查看:109
本文介绍了TabHost:切换选项卡后,第一个加载的选项卡保留在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用TabLayout + ViewPager时,也会发生同样的事情.

Same stuff happening when I used TabLayout+ViewPager.

我的一个片段有一个带有4个标签的TabHost.只有在激活深色主题(AppCompatDelegate.MODE_NIGHT_YES)时,才会发生以下情况.

One of my Fragments has a TabHost with 4 tabs. The following only happens with dark theme active (AppCompatDelegate.MODE_NIGHT_YES).

当片段第一次显示并且我更改选项卡时,发生了幻影效果,并且第一个选项卡停留在背景中:

When the fragment is shown for the first time and I change the tab, a ghost effect happens and the first tab stays in the background:

默认选项卡和单击哪个选项卡都无所谓-任何组合都会导致此问题:

It does not matter which tab is default and which one is clicked after - any combination causes this:

当我退出片段然后回来时,那就很好了.

When I exit the fragment and come back, then it is fine.

这是组件树:

我这样设置TabHost:

I setup the TabHost like this:

    TabHost host = view.findViewById(R.id.tabHost);
    host.setup();

    TabHost.TabSpec spec = host.newTabSpec("one");
    spec.setContent(R.id.tab1);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_info_white_24));
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("two");
    spec.setContent(R.id.tab2);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_account_balance_white_24));
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("three");
    spec.setContent(R.id.tab3);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_notifications_none_white_24));
    host.addTab(spec);

    //Tab 4
    spec = host.newTabSpec("four");
    spec.setContent(R.id.tab4);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_public_white_24));
    host.addTab(spec);

是什么原因造成的,以及如何解决?似乎第一个加载的标签停留在后台.

What is causing this and how to solve it? It seems like the first loaded tab stays in the background.

推荐答案

是的...我也面临这个问题.

Yes... I face this problem too.

您必须尝试添加需要添加(替换)未添加的布局的布局. 如果添加,则在滑动布局时将添加到当前布局的另一个布局. 只需替换布局即可.

you have to try to add layout you need to inflate(replace) layout not add. if you add then when you swipe layout add to another layout to current layout. just replace layout.

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main, onlinesong_home); //this line
        ft.commit();

编码愉快!

这篇关于TabHost:切换选项卡后,第一个加载的选项卡保留在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆