3

我想在我的 ActionBar 中显示一个微调器,使用ActionBar.NAVIGATION_MODE_LIST,但我希望它根据某些应用程序上下文隐藏/显示。我发现我可以使用 将它从 ActionBar 中删除getActionBar().setNavigationMode(-1),但是我不知道这是否是个好主意。

关于这是否安全或是否有更安全的替代方案的任何反馈?

4

3 回答 3

15

也许这更被接受:

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
于 2013-01-30T22:05:11.537 回答
2

如果您坚持使用ActionBar.NAVIGATION_MODE_LIST,则每次要显示微调器时都必须设置导航侦听器。这显然不是最好的解决方案。

相反,您可能希望使用ActionBar.setCustomView()设置微调器导航(参考)。

这是一些设置微调器的示例代码:

Spinner navigationSpinner = new Spinner(this);
navigationSpinner.setAdapter(yourSpinnerAdapter);
// Here you set navigation listener
navigationSpinner.setOnItemSelectedListener(yourSpinnerNavigationListener); 
getActionBar().setCustomView(navigationSpinner);
getActionBar().setDisplayShowCustomEnabled(true);

然后,当您想要显示/隐藏它时,您只需更改它的可见性:

getActionBar().getCustomView().setVisibility(View.INVISIBLE);
于 2013-07-28T15:32:02.147 回答
0

只需像这样修改您的实现ActionBarDrawerToggle

public void onDrawerSlide(View drawerView, float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);
        if (slideOffset == 0) { // 0 = drawer is closed             
            setActionBarNavigationVisibility(activity, true); //show Tabs when Drawer is closed             
        }
    }

public void onDrawerStateChanged(int newState) {
        super.onDrawerStateChanged(newState);
        //hides Tabs right after Drawer starts opening
        if (DrawerLayout.STATE_DRAGGING == newState || DrawerLayout.STATE_SETTLING == newState) {
            setActionBarNavigationVisibility(activity, false);
        }
    }

where 方法setActionBarNavigationVisibility正在考虑所有导航模式(您可以删除不必要的导航模式的代码):

public static void setActionBarNavigationVisibility(Activity activity, boolean visible) {
    try {
        /* 1. --- If the navigation items are showing in ActionBar directly. We have 3 options Spinner, Tabs, and CustomNav ---
         (When Tabs are showing BELOW ActionBar, is handled at the end) */
        int actionViewResId = Resources.getSystem().getIdentifier("action_bar", "id", "android"); // @see http://stackoverflow.com/questions/20023483/how-to-get-actionbar-view
        View actionBarView = activity.findViewById(actionViewResId); // returns instance of com.android.internal.widget.ActionBarView (inaccessible)
        if (actionBarView != null) {
            int visibility = visible ? View.VISIBLE : View.INVISIBLE; // not GONE, so it still takes space in ActionBar layout

            // handle tabs navigation
            Field mTabScrollViewField = actionBarView.getClass().getDeclaredField("mTabScrollView");
            if (mTabScrollViewField != null) {
                mTabScrollViewField.setAccessible(true);
                View mTabScrollView = (View) mTabScrollViewField.get(actionBarView); // instance of com.android.internal.widget.ScrollingTabContainerView (inaccessible)
                if (mTabScrollView != null)
                    mTabScrollView.setVisibility(visibility);
            }

            // handle Spinner navigation
            Field mSpinnerField = actionBarView.getClass().getDeclaredField("mSpinner"); // resp. mListNavLayout
            if (mSpinnerField != null) {
                mSpinnerField.setAccessible(true);
                View mSpinner = (View) mSpinnerField.get(actionBarView); // instance of android.widget.Spinner
                if (mSpinner != null)
                    mSpinner.setVisibility(visibility);
            }

            // handle Custom navigation
            Field mCustomNavViewField = actionBarView.getClass().getDeclaredField("mCustomNavView"); // resp. mListNavLayout
            if (mCustomNavViewField != null) {
                mCustomNavViewField.setAccessible(true);
                View mCustomNavView = (View) mCustomNavViewField.get(actionBarView);
                if (mCustomNavView != null)
                    mCustomNavView.setVisibility(visibility);
            }
        }
        // 2. --- If the Tabs are BELOW ActionBar (narrow screens) ---          
        ViewParent actionBarContainer = actionBarView.getParent(); // parent of ActionBarView is com.android.internal.widget.ActionBarContainer (inaccessible)
        Field mTabContainerField = actionBarContainer.getClass().getDeclaredField("mTabContainer");
        if (mTabContainerField != null) {
            mTabContainerField.setAccessible(true);
            View mmTabContainer = (View) mTabContainerField.get(actionBarContainer);
            if (mmTabContainer != null)
                mmTabContainer.setVisibility(visible ? View.VISIBLE : View.GONE); // now use GONE, so the mTabContainer below Actionbar does not take space in layout
        }

    } catch (Exception ex) {
        // TODO Handle exception...         
    }
}
于 2014-10-22T13:24:39.110 回答