0

我正在使用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。 –

回答

0

我終於找到了答案。標題從CollapsibleToolbarLayout干擾Toolbar。所以,我們禁用它,它工作正常:

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