我通过创建两个Theme.AppCompat.Light.NoActionBar主题并设置了半透明工具栏colorPrimary属性为透明颜色来。
1)为不透明的工具栏创建一个主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    
    <item name="colorPrimary">#ff212cff</item>
</style>
透明/重叠式工具栏的第二个主题:
<style name="AppTheme.Overlay" parent="AppTheme">
    <item name="colorPrimary">@color/transparent</item>
</style>
2)在活动布局中,将工具栏放在内容后面,以便可以在其前面显示:
<RelativeLayout
    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:orientation="vertical">
    <fragment
        android:id="@+id/container"
        android:name="com.test.CustomFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
3)在AndroidManifest.xml中将透明主题应用于活动
    <activity
        android:name="com.test.TransparentActivity"
        android:parentActivityName="com.test.HomeActivity"
        android:theme="@style/AppTheme.Overlay" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.test.HomeActivity" />
    </activity>
     
              
android:background="@android:color/transparent"