【问题标题】:Stacked ActionBar Tab with Custom View Not Displaying Properly带有自定义视图的堆叠式 ActionBar 选项卡无法正确显示
【发布时间】:2014-02-18 23:56:20
【问题描述】:

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

这是下拉列表的截图,在选择一个项目之前:(注意选项卡的内容不显示,即使选项卡被选中)

另外,选择item后,标签不再堆叠,标签内容为空:

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

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

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    这是一个已知的错误,并且已经报告给错误跟踪器:

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

    为了解决这个问题,我用一个讨厌的 hack 禁用了操作栏折叠行为。应从 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.v7.app.ActionBar
    • 很抱歉,但是使用 getSupportActionBar() 插入 getActionBar() 的事件不起作用;我仍然不知道该怎么办。
    【解决方案2】:

    编辑:最终使用

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

    而不是 ActionBar 已弃用的选项卡。 这不会在方向更改时折叠标签栏,但似乎 google 和 facebook 的应用程序也这样做(我猜)。

    更新:似乎它没有用,这只是一个副作用。 似乎是时间问题->当 onConfigurationChanged 中的代码需要一点时间时,问题似乎消失了。 这个非常肮脏的黑客终于奏效了,但它不是一个解决方案(不推荐,也许是其他人找到它的提示):

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       super.onConfigurationChanged(newConfig);
    
    }
    

    对我来说,这在我的 Fragment 中调用时有效。希望对您有所帮助。

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

    【讨论】:

      【解决方案3】:

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

      @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);
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-10
        相关资源
        最近更新 更多