【发布时间】:2014-09-10 14:54:59
【问题描述】:
我有一个 android 应用程序,它有一个主 Activity,它使用许多切换到视图中的片段。我不确定这是否是正确的做法,但我继承了这个项目,并希望避免进行任何重大的重构,例如将片段更改为所有活动或类似的东西。
根据 android 文档,看起来调用 setDisplayHomeAsUp(bool) 函数应该默认只显示向上按钮:
设置是否应将主页显示为“向上”可供性。设置这个 如果在您的 UI 中选择“home”返回单个级别,则为 true 而不是回到顶层或首页。
主要问题是当我使用该功能时:
actionBar.setDisplayHomeAsUpEnabled(true);
它不会将打开导航抽屉的按钮设置为“向上”按钮。它只是从侧面删除了“汉堡包”ic_drawer 图标。导航抽屉仍然打开。
这是 NavigationDrawerFragment 的自定义代码(我复制并粘贴了您在 android studio 中创建带有导航抽屉的新应用程序时获得的确切文件):
NavigationDrawerFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); mDrawerListView = (ListView) inflater.inflate( R.layout.fragment_navigation_drawer, container, false); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } }); PopulateAppDrawerList(); return mDrawerListView; } public void PopulateAppDrawerList() { List<AppOption> allApps = getAllApps(); List<AppOption> filteredApps = new ArrayList<AppOption>(); for (int i = 0; i < allApps.size(); i++) { if (allApps.get(i).getLaunchable()) { filteredApps.add(allApps.get(i)); } } NavDrawerListAdapter adapter = new NavDrawerListAdapter(filteredApps, MainActivity.getInstance()); mDrawerListView.setAdapter(adapter); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); }
然后,我有所有扩展“BaseAppFragment”的其他片段,其中包含以下内容:
BaseAppFragment.java
公共类 BaseAppFragment 扩展片段 {
@Override public void onStart() { super.onStart(); MainActivity.getInstance().onSectionAttached(this); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } }
这使我可以在一个区域中更改操作栏上的标题,并设置是否默认设置后退按钮。
MainActivity.java
public void onSectionAttached(android.app.Fragment 片段) { 类 fragmentType = fragment.getClass();
ActionBar actionBar = getActionBar(); mNavigationDrawerFragment.PopulateAppDrawerList(); if (fragmentType != null) { if (fragmentType.equals(AuthenticationFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(MyOptionsFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "My Options"; } else if (fragmentType.equals(GLAuthenticationFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(InitialLoginFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(LoginFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(false); mTitle = "Login"; } else if (fragmentType.equals(DailyOverviewFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); mTitle = "Overview"; } else if (fragmentType.equals(SingleComponentFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); SingleComponentFragment singleComponentFragment = (SingleComponentFragment) fragment; if (singleComponentFragment != null && singleComponentFragment .mComponent != null) { mTitle = String.format("Back To Day %s", singleComponentFragment.mComponent.getDay() + ""); } else { mTitle = ""; } } else if (fragmentType.equals(singleDayOverviewFragment.class)) { actionBar.setDisplayHomeAsUpEnabled(true); mTitle = "Back To Overview"; } } actionBar.setTitle(mTitle); }
标题设置完美运行,调用 setDisplayHomeAsUpEnabled(true) 时没有错误,但仍然没有显示向上按钮。我知道现在除了片段事务中的 addToBackStack(null) 调用之外,我没有设置任何类型的片段导航层次结构,但看起来这段代码应该足以让向上按钮替换导航抽屉按钮。
【问题讨论】:
-
你正在测试哪个 api?
-
你是说SDK吗?我们的目标是 sdk v.17+
标签: android android-navigation