【问题标题】:Unable to make toolbar transparent in Android无法在 Android 中使工具栏透明
【发布时间】:2015-05-29 12:01:53
【问题描述】:

当我尝试将背景设置为透明时,我的工具栏总是保持灰色。 这是我的 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);

我不确定我错过了什么......

【问题讨论】:

  • developer.android.com/training/basics/actionbar/overlaying.html 看看这个链接。这给出了如何覆盖工具栏
  • @arjun 这不是用于操作栏吗?如前所述,我已设置 true
  • 尝试更改自定义样式的父级并将样式设置为活动而不是工具栏。应在活动级别上进行叠加。
  • 我不明白你..你能把代码贴出来吗..
  • 请导航到我评论中的链接并查看。你会被清除

标签: android xml toolbar


【解决方案1】:

我在试图找到解决这个确切问题的方法时遇到了最大的麻烦。我一定已经搜索了几十个帖子,然后空手而归。最后,我碰巧在一篇文章(我忘记了哪个页面)上提到了 Android v7 工具栏需要位于布局的顶部才能使透明度起作用。谢天谢地,它奏效了。所以,希望这对其他人有帮助:

这是你需要的:

  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 里面,它工作了。但是有一个问题 - 工具栏的汉堡菜单图标已到位,但工具栏标题已在 Activity 之间对齐,与其他视图重叠。
  • 我没有。你需要把它放在另一个布局中。
【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多