带有透明工具栏的Android DrawerLayout

标签 android navigation-drawer android-5.0-lollipop

我正在尝试创建一个带有透明 Toolbar(带有汉堡图标)和 NavigationViewDrawerLayout

这是我的 main_activty 的布局:

<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/primary"
        android:background="@color/secondary"/>

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

这就是我定义 app_bar_map 布局的方式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>

最后,这是 onCreate 方法的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapFragment mapFragment = new MapFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, mapFragment);
    fragmentTransaction.commit();

    toolbar = (Toolbar) findViewById(R.id.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();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

这是最终结果:

enter image description here

请注意,透明工具栏上有一个“边框”。我不知道它来自哪里或如何解决这个问题。

有什么想法吗?

最佳答案

感谢@KeLiuyue 我找到了解决这个问题的方法。

这就是我结束 uo 使用 AppBarLayout 所做的事情:

<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:elevation="0dp"
    android:stateListAnimator="@null">

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

相关文章:

c# - 发布 Xamarin Android apk

javascript - 将 Crashlytics 集成到 React Native - Android

android - 如何在 android 4.2 中监控传入的短信

android - 抽屉导航菜单项显示旧的语言环境值

android - 具有透明背景的 View 中的海拔

android - 多级 ExpandableListView 中的项目并不总是可点击

ios - 像谷歌 iOS Material 设计那样的抽屉

ios - 适用于 iOS 的类似 Android 的抽屉导航?

Android L setEnterTransition 不工作

java - Android 应用程序在 Lollipop 上崩溃