Android Appcompat 工具栏不显示导航抽屉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26569395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appcompat Toolbar Not Showing With Navigation Drawer
提问by user1234
I am trying to configure the following in my app:
我正在尝试在我的应用程序中配置以下内容:
- Toolbar (Appcompat v7 version)
- Navigation Drawer
- Pre-LollipopAppcompat v7 Material theme
- 工具栏(Appcompat v7 版本)
- 导航抽屉
- Pre- LollipopAppcompat v7 Material 主题
I followed the instructions here: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
我按照这里的说明操作:http: //android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
However, after declaring .NoActionBar in the theme, as well as putting the toolbar in the layout, my toolbar does not show. What I end up getting is exactly what you'd expect when declaring no action bar -- no action bar. Here is the layout:
但是,在主题中声明 .NoActionBar 后,以及将工具栏放在布局中后,我的工具栏没有显示. 我最终得到的正是您在声明没有操作栏时所期望的 - 没有操作栏。这是布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Toolbar -->
<include layout="@layout/toolbar"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner_main"
android:visibility="gone"
android:textAlignment="center"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:entries="@array/error_loading_content_array"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0px"></FrameLayout>
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name=".NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"/>
Toolbar.xml:
工具栏.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
In MainActivity.java:
在 MainActivity.java 中:
// Load view/layout
setContentView(R.layout.guidelib_activity_main);
// TODO: Toolbar not showing
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Solution
解决方案
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearlayout_root_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Toolbar -->
<!-- Moved up to new LinearLayout root tag -->
<!--<include layout="@layout/toolbar"/>-->
...
回答by alanv
DrawerLayout extends FrameLayout, but you are treating it like a LinearLayout. You can either wrap your tag and the following LinearLayout in another LinearLayout, or you can move your tag.
DrawerLayout 扩展了 FrameLayout,但您将其视为 LinearLayout。您可以将您的标签和以下 LinearLayout 包装在另一个 LinearLayout 中,也可以移动您的标签。
Also, "fill_parent" is deprecated and maps to "match_parent" so you should just use the latter. You can also remove the additional xmlns attributes in your LinearLayout element.
此外,不推荐使用“fill_parent”并映射到“match_parent”,因此您应该只使用后者。您还可以删除 LinearLayout 元素中的其他 xmlns 属性。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Toolbar -->
<include layout="@layout/toolbar"/>
...
Your original layout did not work because the Toolbar was hidden (z-ordered) behind the LinearLayout.
您的原始布局不起作用,因为工具栏隐藏在 LinearLayout 后面(按 z 顺序排列)。