为什么工具栏不显示标题和设置?

问题描述:

我正在使用DrawerLayout并按下其中一项,我想在CoordinatorLayout内显示Toolbar。因为Android的标准ActionBar会产生ToolBar的问题,所以我决定改用Theme.AppCompat.NoActionBar的活动,并决定使用ToolBar而不是ActionBar为什么工具栏不显示标题和设置?

现在,有一个正常的Toolbar在作为ActionBar,当我点击从抽屉中的一个项目,我想与其他自定义Toolbar取代“正常” Toolbar显示Fragment。所以,我这样做,删除,现在是显示正常Toolbar但工具栏不显示标题和溢出菜单:

enter image description here

你可以看到这里的布局层次。 Toolbar被埋在几个ViewGroups内,并且显示不正确。尽管它是这样显示的,但是在单击工具栏上的主菜单时,抽屉布局会打开和关闭。同样,图标在抽屉打开/关闭时不会改变。

我试图明确设置标题没有用。有谁知道,我在这里做错了什么。


这里是参考代码:

//代码为片段布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/main.appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:title="@string/app_name" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:elevation="4dp" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

      </android.support.v7.widget.Toolbar> 
     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

    <de.hdodenhof.circleimageview.CircleImageView 
     android:id="@+id/profile_pic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/avatar_boy_2" 
     app:layout_anchor="@id/main.appbar" 
     app:layout_anchorGravity="bottom|center"/> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_anchor="@id/profile_pic" 
     app:layout_anchorGravity="bottom" 
     > 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:id="@+id/user_name" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:lineSpacingExtra="8dp" 
       android:padding="@dimen/activity_horizontal_margin" 
       android:text="@string/account_person_name_label" 
       android:textAlignment="center" 
       android:textSize="40sp" 
       app:layout_constraintHorizontal_bias="0.0" 
       app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintRight_toRightOf="parent" 
       tools:layout_editor_absoluteY="168dp" /> 

     </android.support.constraint.ConstraintLayout> 

    </android.support.v4.widget.NestedScrollView> 


</android.support.design.widget.CoordinatorLayout> 
+0

什么是所需的输出? – azizbekian

+0

@azizbekian:所需的输出是有一个正常的工具栏,它显示文本,溢出菜单作为一个不变的ActionBar。 –

我终于找到了答案。标题从CollapsibleToolbarLayout干扰Toolbar。所以,我们禁用它,它工作正常:

collapsingToolbarLayout.setTitleEnabled(false); 
toolbar.setTitle("My Title");