堆积动作条标签与自定义视图显示不正常 [英] Stacked ActionBar Tab with Custom View Not Displaying Properly

查看:102
本文介绍了堆积动作条标签与自定义视图显示不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用操作栏选项卡,有时显示为堆叠当标签内容是过大的显示屏。当我使用自定义视图选项卡的内容出现问题,它会导致所选择的选项卡不显示在下拉列表中,而一旦标签被选中,在下拉菜单消失,而且体积小,空选项卡出现。

下面是下拉的屏幕截图,选择项目之前:(请注意,不显示选项卡的内容,即使选择的标签)

另外,选择项后,翼片不再堆叠,且选项卡的内容是空的:

下面是我的code,(请注意,我使用的是自定义视图的标签只是为了说明问题)

 公共类为ExampleActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        最后的TextView selectedTabText =新的TextView(本);
        的setContentView(selectedTabText);

        ActionBar.TabListener监听器=新ActionBar.TabListener(){
            @覆盖
            公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction英尺){
                TextView的customView =(TextView中)tab.getCustomView();
                selectedTabText.setText(customView.getText());
            }

            @覆盖
            公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction英尺){

            }

            @覆盖
            公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction英尺){

            }
        };

        动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        addTab(动作条,听众,标签之一,一个很长的名字);
        addTab(动作条,听众,标签两次与一个很长的名字);
        addTab(动作条,听众,标签三连一个很长的名字);
        addTab(动作条,听众,标签四连一个很长的名字);
    }

    私人无效addTab(动作条动作条,ActionBar.TabListener监听器,文本字符串){
        ActionBar.Tab标签= actionBar.newTab();
        TextView中的TextView =新的TextView(本);
        textView.setText(文本);
        tab.setCustomView(TextView的);
        tab.setTabListener(听众);
        actionBar.addTab(标签);
    }
}
 

解决方案

这是一个已知的bug,并已上报bug跟踪系统:

HTTPS://$c$c.google。 COM / P /安卓/问题/详细信息?ID = 41392

在为了解决这个问题,我禁用了动作条倒塌行为有一个讨厌的黑客攻击。下面的方法应该从ONSTART活动的方法来调用:

  / **
 *超级黑客禁用标签在较小的屏幕,它不允许使用自定义标签栏的观点崩溃
 * HTTPS://$c$c.google.com/p/android/issues/detail ID = 41392
 * /
私人无效disableCollapsibleTabs(){
    尝试 {
        动作条动作条= getActionBar();
        现场actionViewField = actionBar.getClass()getDeclaredField(mTabScrollView)。
        actionViewField.setAccessible(真正的);
        对象mTabScrollView = actionViewField.get(动作条);

        方法setAllowCollapse = mTab​​ScrollView.getClass()getDeclaredMethod(setAllowCollapse,boolean.class)。
        setAllowCollapse.setAccessible(真正的);
        setAllowCollapse.invoke(mTabScrollView,假);

    }赶上(例外五){
        Log.e(同时禁用动作条可折叠标签,E,错误);
    }
}
 

When using action bar tabs, sometimes they are displayed as "stacked" when tab content is too large for the display. A problem arises when I use a custom view for the tab content, it causes the selected tab to not be displayed in the dropdown, and once a tab is selected, the dropdown goes away, and small, empty tabs appear.

Here is a screenshot of the dropdown, before selecting an item: (note that the content of the tab is not displayed, even when the tab is selected)

Also, after selecting the item, the tabs are no longer stacked, and the content of the tabs is empty:

Here is my code, (note that I am using a custom view for the tabs just to demonstrate the problem)

public class ExampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TextView selectedTabText = new TextView(this);
        setContentView(selectedTabText);

        ActionBar.TabListener listener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                TextView customView = (TextView) tab.getCustomView();
                selectedTabText.setText(customView.getText());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        addTab(actionBar, listener, "Tab one with a very long name");
        addTab(actionBar, listener, "Tab two with a very long name");
        addTab(actionBar, listener, "Tab three with a very long name");
        addTab(actionBar, listener, "Tab four with a very long name");
    }

    private void addTab(ActionBar actionBar, ActionBar.TabListener listener, String text) {
        ActionBar.Tab tab = actionBar.newTab();
        TextView textView = new TextView(this);
        textView.setText(text);
        tab.setCustomView(textView);
        tab.setTabListener(listener);
        actionBar.addTab(tab);
    }
}

解决方案

This is a known bug, and has already been reported to the bug tracker:

https://code.google.com/p/android/issues/detail?id=41392

In order to work around this, I disabled the actionbar collapsing behavior with a nasty hack. The following method should be called from the onStart activity method:

/**
 * 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);
    }
}

这篇关于堆积动作条标签与自定义视图显示不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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