【问题标题】:Android Toolbar set background color to semitransparent colorAndroid工具栏将背景颜色设置为半透明颜色
【发布时间】:2017-06-02 02:06:25
【问题描述】:

是否可以在谷歌地图上将工具栏背景颜色设置为半透明颜色。

我尝试了两件事:

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")));

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

【问题讨论】:

  • 如您所展示的那样在 XML 中执行应该可以。我今天刚做了。

标签: java android


【解决方案1】:

如果您将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>>

结果:

【讨论】:

  • 谢谢!这应该可行,但我对主题有疑问。 @style/AppTheme.NoActionBar.AppBarOverlay @style/AppTheme.NoActionBar.PopupOverlay 刚加了,工具栏就不是半透明了
  • 将主题更改为 @style/AppTheme.PopupOverlay 就像你拥有它应该仍然有效,请参阅编辑后的答案
  • 问题仍然反复出现:i.imgur.com/3THFN6E.png 它必须像您的图像一样透明,但在我的应用程序上,它仍然在顶部显示一个白色层,其中包含您显示的任一主题
  • 您的 mapView 是否会直接显示在屏幕顶部?我猜你看到的白色是来自AppBarLayout下的视图
  • 是的。我不希望地图视图向下移动,因为我希望工具栏透明的原因是看到它下面的地图视图
猜你喜欢
  • 2020-07-21
  • 1970-01-01
  • 2020-03-03
  • 2016-11-06
  • 2011-02-11
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多