首页 > 解决方案 > 如何更改工具栏标高颜色?

问题描述

在应用程序中有一个更改主题颜色的选项,根据主题颜色,我正在使用以下行更改工具栏背景的颜色:

toolbar.setBackgroundColor(getResources().getColor(R.color.white));

但是,我找不到任何可以帮助我改变海拔颜色的东西。这是工具栏xml:

<com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp">

        <RelativeLayout
            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_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/headset"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="@string/contact_us"
                android:textColor="@color/gray"
                android:textSize="18sp" />

        </RelativeLayout>

    </androidx.appcompat.widget.Toolbar>

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

请让我知道如何以编程方式进行。

标签: androidandroid-toolbarandroid-elevation

解决方案


设置 AppBarLayout 高程

<android.support.design.widget.AppBarLayout
    app:elevation="0dp">

应用主题

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="colorPrimaryDark">@color/green_tataki</item>
    </style>

推荐阅读