如何在整个应用程序中使用自定义工具栏

How to use Custom Toolbar in entire app

我在我的应用程序中使用坐标工具栏制作了工具栏,这样我可以在滚动时隐藏 toolbar。我也有 ViewPager 里面。因此,当用户滚动时,只有工具栏会隐藏,而选项卡会粘在顶部。但在其他 activity 中,我只是 想显示工具栏,什么是更好的方法

我在下面分享我的协调器布局

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

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


    <android.support.v7.widget.Toolbar                 
xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/orange"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@color/white"> 
</android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/MyTabLayout"
        style="@style/TabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        app:tabTextAppearance="@style/AppTabTextAppearance">



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

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




    <android.support.v4.view.ViewPager
        android:id="@+id/MyViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/MyTabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        ></android.support.v4.view.ViewPager>



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

如果您只想在其他 Activity 中显示工具栏,您可以将工具栏添加到相应 Activity 的任何根布局,例如 LinearLayout 或 RelativeLayout(适合您应用的需要)。

仅为工具栏创建 separate 布局。

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/white"
    app:layout_scrollFlags="scroll|enterAlways"
    app:titleTextColor="@color/white">


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

那么现在它位于您布局中的任何位置。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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


        <include
            layout="@layout/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <android.support.design.widget.TabLayout
            android:id="@+id/MyTabLayout"
            style="@style/TabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            app:tabTextAppearance="@style/AppTabTextAppearance">


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

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


    <android.support.v4.view.ViewPager
        android:id="@+id/MyViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/MyTabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>


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