Appcompat工具栏不显示导航抽屉 [英] Appcompat Toolbar Not Showing With Navigation Drawer
问题描述
我想配置我的应用程序如下:
- 工具栏(Appcompat V7版本)
- 在导航抽屉
- pre型的棒棒堂的Appcompat V7材料主题
我跟着指示的位置:<一href="http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html">http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html
不过,声明.NoActionBar的主题,以及把工具栏上的布局后,我的工具栏不显示。我最终得到完全宣称没有行动起来吧,当你期望的那样 - 不采取行动吧。这里是布局:
&LT; android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。&GT;
&LT;! - 工具栏 - &GT;
&LT;包括布局=@布局/工具栏/&GT;
&LT; LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / layout_main
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
机器人:方向=垂直&GT;
&LT;微调
机器人:ID =@ + ID / spinner_main
机器人:能见度=水涨船高
机器人:textAlignment =中心
机器人:重力=中心
机器人:layout_gravity =center_horizontal
机器人:项=@阵列/ error_loading_content_array
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/&GT;
&LT;的FrameLayout
机器人:ID =@ + ID /容器
机器人:layout_weight =1
机器人:layout_width =match_parent
机器人:layout_height =0px&GT;&LT; /的FrameLayout&GT;
&LT; / LinearLayout中&GT;
&LT;片段
机器人:ID =@ + ID / navigation_drawer
机器人:layout_width =@扪/ navigation_drawer_width
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:名称=。NavigationDrawerFragment
工具:布局=@布局/ fragment_navigation_drawer/&GT;
Toolbar.xml:
&LT; android.support.v7.widget.Toolbar
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID /工具栏
应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
机器人:=了minHeight?ATTR / actionBarSize
机器人:ATTR / colorPrimary后台=
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/&GT;
在MainActivity.java:
//加载视图/布局
的setContentView(R.layout.guidelib_activity_main);
// TODO:工具栏不显示
mToolbar =(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
getSupportActionBar()setHomeButtonEnabled(真)。
解决方案
&LT;的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / linearlayout_root_main
机器人:layout_width =match_parent
机器人:layout_height =match_parent&GT;
&LT; android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。&GT;
&LT;的LinearLayout
机器人:ID =@ + ID / layout_main
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直&GT;
&LT;! - 工具栏 - &GT;
&LT;! - 上升到新的LinearLayout根标签 - &GT;
&LT;! - &LT;包括布局=@布局/工具栏/&GT; - &GT;
...
DrawerLayout延伸的FrameLayout,但是你对待它像一个LinearLayout中。你可以换你的标签,并在另外的LinearLayout以下的LinearLayout,或者您也可以将您的标记。
此外,FILL_PARENT是德precated和地图match_parent所以,你应该只使用后者。您还可以删除额外的xmlns在你的LinearLayout元素的属性。
&LT; android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。&GT;
&LT;的LinearLayout
机器人:ID =@ + ID / layout_main
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直&GT;
&LT;! - 工具栏 - &GT;
&LT;包括布局=@布局/工具栏/&GT;
...
您原来的布局没有工作,因为该工具栏被隐藏(Z排序)中的LinearLayout后面。
I am trying to configure the following in my app:
- Toolbar (Appcompat v7 version)
- Navigation Drawer
- Pre-Lollipop Appcompat v7 Material theme
I followed the instructions here: 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:
<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:
<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:
// 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"/>-->
...
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.
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.
<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.
这篇关于Appcompat工具栏不显示导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!