在Android中无法使工具栏透明

3

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

这是我的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" />

我的主题
 <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>

我已经从代码中尝试过它。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        toolbar.setBackgroundResource(Color.TRANSPARENT);

我不确定自己缺少什么...


看一下这个链接:http://developer.android.com/training/basics/actionbar/overlaying.html。它介绍了如何叠加工具栏。 - arjun
@arjun 这不是用于操作栏吗?我已经按照所述设置了<item name="android:windowActionBarOverlay">true</item>。 - Prasanna Aarthi
尝试更改自定义样式的父级并将样式设置为活动而不是工具栏。叠加应在活动级别上完成。 - arjun
我不明白你的意思,请发一下代码好吗? - Prasanna Aarthi
请点击我评论中的链接查看,这样你就会明白了。 - arjun
2个回答

11

我曾经尝试找到解决这个问题的方法,但却遇到了最大的困难。我阅读了很多文章,但是都没有得到满意的答案。最终,我在一篇文章中(我忘记了是哪一页)找到了答案:Android v7 Toolbar必须放在布局的顶部才能使透明度生效。幸运的是,它起作用了。希望这能帮助到其他人:

以下是需要做的几步:

  1. 像下面这样为活动定义一个布局。
  2. 确保工具栏位于XML底部,以便在Z轴次序上处于顶部位置。
  3. 使用RelativeLayout,以便您可以确保工具栏在屏幕左上角视觉上位于顶部。
  4. @color/primary 应该是透明的(#00000000),或者如果您需要使用其他颜色,则可以在代码中设置它。
  5. (可选)要么在下面的容器中添加"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"
        />
    

你如何使用多个根标签?我收到错误信息:“多个根标签”。我将其放在DrawerLayout中,然后它可以正常工作。但是有一个问题——工具栏的汉堡菜单图标已经显示了,但是工具栏标题却与活动页面重叠了。 - gegobyte
我没有这样做。你需要将它放在另一个布局中。 - MCLLC

2

最新的Android X更新:

使用@color/transparent = #00000000

用法:

android:background="@color/transparent" 
android:elevation="0dp"
app:elevation="0dp"

在应用栏上
示例布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                android:background="@color/transparent"
                android:elevation="0dp"
                app:elevation="0dp"
                >

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:titleMarginStart="@dimen/margin_big"

                    />

            </com.google.android.material.appbar.AppBarLayout>

            <fragment
                android:id="@+id/nav_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/nav_main"
                />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@color/bottom"
                app:menu="@menu/navigation"
                app:layout_constraintBottom_toBottomOf="parent"
                />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/navigation"
            />

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接