2

我在CoordinatorLayout中使用CollapsingToolbarLayout來實現我的bottomomsheet佈局。佈局看起來是這樣的 -如何在Bottomsheet佈局中自定義工具欄?

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/bottomSheet" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/darkBackgroundMainTranslucent" 
    app:behavior_hideable="true" 
    app:layout_behavior="@string/bottom_sheet_behavior"> 

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

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      android:fitsSystemWindows="true" 
      app:contentScrim="@color/colorScrim" 
      app:expandedTitleMarginStart="48dp" 
      app:expandedTitleMarginEnd="64dp"> 

      <ImageView 
       android:id="@+id/backdrop" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:fitsSystemWindows="true" 
       android:contentDescription="@string/poster_string" 
       app:layout_collapseMode="parallax" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/bottomsheet_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:popupTheme="@style/AppTheme.PopupOverlay" 
       app:layout_collapseMode="pin" /> 

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

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

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/nestedScrollView"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:paddingTop="24dp"> 

      <android.support.v7.widget.CardView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="@dimen/card_margin"> 

       <LinearLayout 
        style="@style/Widget.CardContent" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Title" 
         android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 

        <TextView 
         android:id="@+id/bottomsheet_title" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textIsSelectable="true"/> 

       </LinearLayout> 

      </android.support.v7.widget.CardView> 


     </LinearLayout> 

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

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

我不知道這是否有可能定製CollapsingToolbarLayout裏面的工具欄沒有設置該工具欄的supportactionbar添加了按鈕,並設置其標題。

回答

0

你可以把圖標/圖像/按鈕工具欄裏面,通過這樣的XML:

 Toolbar bottomSheetToolbar = (Toolbar) bottomSheetLayout.findViewById(R.id.bottomsheet_toolbar); 
     ImageButton contact_detail_edit = (ImageButton)bottomSheetToolbar.findViewById(R.id.contact_detail_edit); 
     ImageButton contact_detail_favorite = (ImageButton)bottomSheetToolbar.findViewById(R.id.contact_detail_favorite); 

<android.support.v7.widget.Toolbar 
      android:id="@+id/bottomsheet_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:layout_collapseMode="pin"> 

      <ImageButton 
        ... your usual crud height/width ... 
        android:id="@+id/contact_detail_edit/> 

      <TextView /> 

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

然後通過工具欄,像這樣訪問它們