Android 带有自定义视图的堆叠操作栏选项卡未正确显示

Android 带有自定义视图的堆叠操作栏选项卡未正确显示,android,android-actionbar,Android,Android Actionbar,使用操作栏选项卡时,有时当选项卡内容太大而无法显示时,它们会显示为“堆叠”。当我对选项卡内容使用自定义视图时,会出现一个问题,它会导致所选选项卡不显示在下拉列表中,并且一旦选择了选项卡,下拉列表就会消失,出现小的空选项卡 以下是选择项目前下拉列表的屏幕截图:(注意,即使选择了选项卡,也不会显示选项卡的内容) 此外,选择项目后,选项卡不再堆叠,选项卡内容为空: 这是我的代码,(请注意,我正在使用选项卡的自定义视图来演示问题) 这是一个已知的bug,已经向bug追踪器报告: 为了解决这个问题,

使用操作栏选项卡时,有时当选项卡内容太大而无法显示时,它们会显示为“堆叠”。当我对选项卡内容使用自定义视图时,会出现一个问题,它会导致所选选项卡不显示在下拉列表中,并且一旦选择了选项卡,下拉列表就会消失,出现小的空选项卡

以下是选择项目前下拉列表的屏幕截图:(注意,即使选择了选项卡,也不会显示选项卡的内容)

此外,选择项目后,选项卡不再堆叠,选项卡内容为空:

这是我的代码,(请注意,我正在使用选项卡的自定义视图来演示问题)


这是一个已知的bug,已经向bug追踪器报告:

为了解决这个问题,我通过一次恶意攻击禁用了actionbar崩溃行为。应从onStart活动方法调用以下方法:

/**
 * SUPER hack to disable tab collapsing with smaller screens, which doesn't allow custom tab bar views to be used
 * https://code.google.com/p/android/issues/detail?id=41392
 */
private void disableCollapsibleTabs() {
    try {
        ActionBar actionBar = getActionBar();
        Field actionViewField = actionBar.getClass().getDeclaredField("mTabScrollView");
        actionViewField.setAccessible(true);
        Object mTabScrollView =  actionViewField.get(actionBar);

        Method setAllowCollapse = mTabScrollView.getClass().getDeclaredMethod("setAllowCollapse", boolean.class);
        setAllowCollapse.setAccessible(true);
        setAllowCollapse.invoke(mTabScrollView, false);

    } catch (Exception e) {
        Log.e("", "Error while disabling actionbar collapsible tabs", e);
    }
}
编辑:最终使用

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tint" />`

对我来说,这在我的片段中起作用。希望能有帮助

@Override
public void onConfigurationChanged(Configuration newConfig) {
    final ActionBar actionBar = getActivity()).getSupportActionBar();
    actionBar.invalidateOptionsMenu();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {

    try {
        Thread.sleep(100);
        actionBar.getTabAt(0).setCustomView(/*your custom view*/);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    super.onConfigurationChanged(newConfig);


}

调用onConfigurationChanged方法时,只需重置自定义选项卡。 它对我有用。 希望能有帮助

@Override
public void onConfigurationChanged(Configuration newConfig) {
    final ActionBar actionBar = getActivity()).getSupportActionBar();
    actionBar.invalidateOptionsMenu();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {

    try {
        Thread.sleep(100);
        actionBar.getTabAt(0).setCustomView(/*your custom view*/);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    super.onConfigurationChanged(newConfig);


}

它不适用于android.support.v7.app.ActionBar抱歉,但它不适用于使用getSupportActionBar()而不是getActionBar()的事件;我还是不知道该怎么办。