如何在Bottomsheet布局中自定义工具栏?

时间:2016-03-17 12:29:28

标签: android android-support-library android-toolbar android-collapsingtoolbarlayout bottom-sheet

我在CoordinatorLayout中使用CollapsingToolbarLayout作为我的底片的布局。布局看起来像这样 -

<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。

1 个答案:

答案 0 :(得分:0)

您可以通过xml将图标/图像/按钮放在工具栏中,如下所示:

<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>

然后通过工具栏访问它们,如下所示:

        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);