java - Android-透明工具栏

标签 java android xml transparency toolbar

我正在尝试为我的应用程序菜单制作一个带有汉堡包图标的透明工具栏,但该工具栏仍保持灰色,并带有汉堡包图标和我的应用程序名称。

我希望 map fragment 是透明的,就像 Uber 应用程序主屏幕一样。

这是我到目前为止所做的:

样式.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>

在 list 上我设置了主题:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

...

activity_main.xml

<android.support.v4.widget.DrawerLayout 
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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        app:itemTextColor="@color/colorTextView"
        android:background="@color/colorLightTextView"/>

</android.support.v4.widget.DrawerLayout>

app_bar_map.xml

<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".core.MainActivity">

    <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"
        app:elevation="0dp">

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

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize" />

</android.support.design.widget.CoordinatorLayout>

在我的 MainActivity.java 上的 onCreate 方法上:

...
super.onCreate(savedInstanceState);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.app_name, R.string.app_name);
drawer.setDrawerListener(toggle);
toggle.syncState();
...

最佳答案

我明白了!

我意识到我看到的灰色部分是CoordinatorLayout

之后,我很容易意识到自己的错误。我将 FrameLayout 放在工具栏下方,并显示了 CoordinatorLayout 的一部分。

因此,我从 FrameLayout 中删除了这一行 android:layout_marginTop="?android:attr/actionBarSize" 并进行了一些调整以确保工具栏呈现在上方FrameLayout

最终xml:

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".core.MainActivity">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

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

</RelativeLayout>

关于java - Android-透明工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45652601/

相关文章:

java - 如何通过 d :date properties in Alfresco? 启用高级搜索

java - 如何使用 GeoDataApi.getPlaceById?

java - 在 android 4.4 中注意到 float 操作按钮周围有奇怪的圆圈吗?

android - xml中 fragment 中ListView上方的 float 操作按钮(FAB)?

java - Java 中有没有办法读取文本文件中的换行符?

java - Jetty 9 - 如何使用 Jetty 9 设置 "request content"和 "request headers"?

Java - 在 for 循环中使用 "continue"时出现未处理的异常错误?

android - 在 onCreate/onDestroy 上注册/注销 BroadcastReceivers

ios - 如何从iOS写入XML文件?

c# - 如何使用 XMLDocument 类从 C# 中的 XML 文件获取数据?