无法在Android中使工具栏透明

Pra*_*thi 2 xml android toolbar

当我尝试将背景设置为透明时,我的工具栏始终保持灰色.这是我的XML.

<android.support.v7.widget.Toolbar 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="wrap_content"
    android:background="@android:color/transparent"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    app:theme="@style/Rushmore.Toolbar.Transparent" />
Run Code Online (Sandbox Code Playgroud)

而我的主题

 <style name="Rushmore.Toolbar.Transparent" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:windowActionBarOverlay">true</item>

        <!-- Support Library compability -->
        <item name="windowActionBarOverlay">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

我从代码中尝试过它

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        toolbar.setBackgroundResource(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)

我不知道我错过了什么......

MCL*_*LLC 8

我试图找到解决这个问题的方法遇到了最大的麻烦.我一定要搜索过几十个帖子然后空手而归.最后,我发现了一个帖子(我忘了哪个页面),提到Android v7工具栏需要在布局的顶部才能实现透明性.谢天谢地,它奏效了.所以,希望这将有助于其他人:

这就是你需要的:

  1. 为您的活动定义布局,如下所示
  2. 确保工具栏位于XML的底部,因此它将位于z顺序的顶部.
  3. 使用RelativeLayout,以确保工具栏位于屏幕左上角.
  4. @ color/primary应该是透明的(#00000000)或者你可以在代码中设置它,如果你也需要使用其他颜色.
  5. (OPTIONALLY)将"android:layout_marginTop ="?android:attr/actionBarSize" 添加到下面的容器中或者将其添加到代码中.否则,FrameLayout中的某些内容将位于操作栏下方.

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
        <ListView android:id="@+id/left_drawer"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginRight="8dp"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="@color/drawer_background"
            />
    
    </android.support.v4.widget.DrawerLayout>
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/primary"
        />
    
    Run Code Online (Sandbox Code Playgroud)