java - Android 工具栏设置背景颜色为半透明颜色

标签 java android

是否可以将 Google map 顶部的工具栏背景颜色设置为半透明颜色。

我尝试了两件事:

XML:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#963f51b5"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

Java:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);
getSupportActionBar().setTitle(distanceText);

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#963f51b5")));

似乎都不起作用,这让我觉得这是不可能的?

最佳答案

如果将 AppBarLayout 的背景设置为透明,它应该可以工作。

例如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">

    <LinearLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--Add the rest of your views in here-->
    </LinearLayout>

    <!--Needs to be at the bottom so it's shown over all the other views-->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#963f51b5"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
</RelativeLayout>>

结果:

enter image description here

关于java - Android 工具栏设置背景颜色为半透明颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41706819/

相关文章:

java - 编译时错误 - 在 import java.io.BufferedReader 中找不到符号;

java - JavaFX 中的 SubScene 不显示从 FXML 文件加载的元素

java - Eclipse Juno-WindowBuilder 给出解析错误(java.lang.IllegalArgumentException)

android - setdropdownview 预期资源标识符

android - 使用 Android Volley 在网站的搜索栏上执行搜索

android - 创建 Android Library 项目 jar

java - 当另一个对话框打开时不返回对话框

java - 在本地模式下运行多个storm拓扑

android - Koin 无法从已发布的库中创建类的实例

android - 实现 HTML 书状分页