无法使工具栏透明的Andr​​oid工具栏、透明、oid、Andr

2023-09-04 23:42:17 作者:樱桃味的萝莉

我的工具栏始终保持灰色当我尝试设置背景为透明。 这里是我的XML。

My tool bar always stays gray when I try to set the background as transparent. Here is my 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>

我试图从code

I have tried it from code

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        toolbar.setBackgroundResource(Color.TRANSPARENT);

我不知道它是什么我失踪......

I am not sure what is it I am missing...

推荐答案

我有最坏的麻烦试图找到一个解决这个确切的问题。我必须搜查几十个职位,并拿出空手而归。最后,我偶然发现了一个帖子(我忘记是哪个页面),其提及的是,Android的V7工具栏必须是在布局之上,以透明的工作。值得庆幸的是,它的工作。所以,我希望这将帮助别人:

I was having the worst trouble trying to find a solution to this exact issue. I must have searched dozens of posts and come up empty handed. Finally, I happened on a post (I forget which page) that mentioned that the Android v7 Toolbar needs to be on TOP of the layout in order for transparency to work. Thankfully, it worked. So, hopefully this will help someone else:

下面是你需要的:

定义布局您的活动像下面 确保工具栏是在XML的底部,因此这将是在顶部的z顺序。 使用RelativeLayout的,所以你可以确保工具栏在视觉上左上角的屏幕上。 @色/初级应该是透明的(#00000000),或者如果你需要使用其他颜色也可以将其设置在code。

(可选)为ADD机器人:layout_marginTop =机器人:?ATTR / actionBarSize下面的容器的或的在code添加它。否则,一些在的FrameLayout内容将是操作栏下方。 Define a layout for your activity like below Make sure the Toolbar is at the bottom of the XML so it'll be on top in the z-order. Use RelativeLayout so you can make sure the Toolbar is visually at the top left on the screen. @color/primary should be transparent (#00000000) OR you can set it in code if you need to use other colors as well.

(OPTIONALLY) Either add "android:layout_marginTop="?android:attr/actionBarSize" to the container below OR add it in your code. Otherwise, some of the content in the FrameLayout will be underneath the action bar.

<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"
    />