Android 默认情况下,工具栏背景为白色,即使背景未设置为白色

Android 默认情况下,工具栏背景为白色,即使背景未设置为白色,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,我在我的活动中使用了一个工具栏,其中我没有设置任何类型的白色作为背景,但即使我没有设置任何背景,它仍然是白色的,我想要的是,白色应该从工具栏中删除,它应该加载我试图设置的背景。请查看我的代码: background_toolbar.xml: <gradient android:angle="270" android:endColor="@android:color/transparent" android:startColor="#66000000"/> &

我在我的活动中使用了一个工具栏,其中我没有设置任何类型的白色作为背景,但即使我没有设置任何背景,它仍然是白色的,我想要的是,白色应该从工具栏中删除,它应该加载我试图设置的背景。请查看我的代码:

background_toolbar.xml:

<gradient
    android:angle="270"
    android:endColor="@android:color/transparent"
    android:startColor="#66000000"/>
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:elevation="0dp"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    app:layout_scrollFlags="scroll|enterAlways">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="156dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Table List"
            android:textColor="#679643"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.555" />

              <ImageView
            android:id="@+id/mfilter"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:src="@drawable/filter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.975"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.652" />
    </android.support.constraint.ConstraintLayout>
            </android.support.v7.widget.Toolbar>


toolbar.xml:

<gradient
    android:angle="270"
    android:endColor="@android:color/transparent"
    android:startColor="#66000000"/>
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:elevation="0dp"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    app:layout_scrollFlags="scroll|enterAlways">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="156dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Table List"
            android:textColor="#679643"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.555" />

              <ImageView
            android:id="@+id/mfilter"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:src="@drawable/filter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.975"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.652" />
    </android.support.constraint.ConstraintLayout>
            </android.support.v7.widget.Toolbar>


我认为您没有将
android:background
属性添加到工具栏中

如果要使工具栏透明,可以尝试添加:

android:background="@null"

如果你想设置一些特定的背景,那么:

android:background="@drawable/your_background_file"
它应该能解决你的问题


希望有帮助

我认为您没有将
android:background
属性添加到工具栏中

如果要使工具栏透明,可以尝试添加:

android:background="@null"

如果你想设置一些特定的背景,那么:

android:background="@drawable/your_background_file"
它应该能解决你的问题


希望有帮助

这可能是因为您的样式名称中包含一些不需要的值。请检查您的
值-21
。在我的例子中,状态栏总是透明的,因为
values-21
中有以下样式(在Android Studio中创建项目时生成):


假的
真的
@android:彩色/透明

这可能是因为您的样式名称中包含一些不需要的值。请检查您的
值-21
。在我的例子中,状态栏总是透明的,因为
values-21
中有以下样式(在Android Studio中创建项目时生成):


假的
真的
@android:彩色/透明

在工具栏上应用主题和背景的位置?工具栏通常将应用程序的原色指定为其背景色,主题的原色为#00000000,您给出了0%的不透明度,因此可能这就是为什么它的显示方式。尝试更改AppTheme中的原色并重建项目在工具栏中添加此行
android:background=“#66000000”
。可能会对您有所帮助。在工具栏上应用主题和背景的位置?工具栏通常将应用程序的原色指定为其背景色,主题的原色为#00000000,你给了一个0%的不透明度,所以也许这就是为什么它看起来是这样的。尝试更改AppTheme中的原色并重建项目在工具栏中添加此行
android:background=“#66000000”
。可能对你有帮助。是的,你是对的,我希望它是透明的,并尝试了你所说的一切,但工具栏仍然是白色的。如果我试图将操作栏默认设置为透明,它将加载white@Mr.patel为什么不用相对布局替换工具栏,并给它一个透明的背景呢(或任何你想要的背景)然后将文本居中并将过滤器对齐到底?我认为这样做对您来说更容易,但我希望工具栏也能达到这种效果。我已经复制了您的代码,甚至style.xml和工具栏在我的情况下都是透明的。似乎还有其他内容覆盖了它。检查您的样式。xml是的,我希望它是透明的d像你说的那样尝试了所有操作,但工具栏仍然是白色的。如果我试图将操作栏默认设置为透明,则操作栏也是如此white@Mr.patel为什么不用相对布局替换工具栏,并给它一个透明的背景(或任何你想要的背景)然后将文本居中,并将过滤器对齐到底?我认为这样做对您来说更容易,但我希望工具栏也能达到这种效果。我已经复制了您的代码,即使style.xml和工具栏在我的情况下也是透明的。似乎还有其他内容覆盖了它。请检查您的style.xml