仅供参考

XML布局

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"
            app:toolbarId="@+id/toolbar">

            <ImageView
                android:id="@+id/app_bar_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/image3"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="80dp">
            </android.support.v7.widget.Toolbar>

        </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="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="100dp">
            
            <!-- 此处为随动列表 -->

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

</android.support.design.widget.CoordinatorLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_exp1"
        android:title="temp1"/>
    <item
        android:icon="@drawable/ic_share"
        android:title="share"
        android:id="@+id/item_exp2"
        app:showAsAction="always"/>        //常显示
    <item
        android:id="@+id/item_exp3"
        android:title="temp2"/>
    <item
        android:title="temp3"
        android:id="@+id/item_exp4"/>

</menu>

代码

设置toolbar

setContentView(R.layout.activity_first);

后添加

Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("Welcome");
toolbar.setSubtitle("It`s my little app.");


        Toolbar.OnMenuItemClickListener menuItemClick = new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                switch (id){
                    case R.id.item_exp1:
                        //
                        break;
                    case R.id.item_exp2:
                       //
                        break;
                    case R.id.item_exp3:
                        //
                        break;
                    case R.id.item_exp4:
                        //
                        break;
                }
                return false;
            }
        };

toolbar.setOnMenuItemClickListener(menuItemClick);        //放置menu
toolbar.inflateMenu(R.menu.settings);

版权声明:本文为Mr-quin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Mr-quin/p/8583507.html