简体   繁体   English

Android-防止导航抽屉覆盖标题栏

[英]Android - Prevent navigation drawer from overlaying title bar

I recently opened a project in Android Studio and selected a Navigation Drawer Activity. 我最近在Android Studio中打开了一个项目,然后选择了“导航抽屉活动”。 It created the navigation drawer, except that it's being displayed in a way that I don't like - the navigation drawer is displayed on top of the title bar, for example: 它创建了导航抽屉,只是它以我不喜欢的方式显示-导航抽屉显示在标题栏的顶部,例如:

Instead, I'd like it to be like: 相反,我希望它像:

In this "correct" example, the title bar - "Home" is being displayed, alongside a closing arrow, while the navigation drawer opens. 在此“正确”的示例中,导航栏打开时,标题栏“主页”与关闭的箭头一起显示。

File sources: 文件来源:

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.avi12.soundcloudinstantdownloader.MainActivity"
    android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

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

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.avi12.soundcloudinstantdownloader.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="What this app can download:"/>

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="17dp"
        android:layout_marginStart="17dp"
        android:text="Single tracks"/>

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_alignStart="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:text="Playlists, a.k.a albums"/>

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView9"
        android:layout_alignStart="@+id/textView9"
        android:layout_below="@+id/textView9"
        android:text="User likes (though only some, due to SoundCloud limiting their API)"/>

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView10"
        android:layout_marginTop="12dp"
        android:text="Instantly download songs by sharing it from SoundCloud with this app!"/>

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView11"
        android:layout_alignStart="@+id/textView11"
        android:layout_below="@+id/textView11"
        android:layout_marginTop="18dp"
        android:text="Alternatively, you can search the song, and even paste its URL:"/>

    <EditText
        android:id="@+id/inputDownload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView12"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignStart="@+id/textView12"
        android:layout_below="@+id/textView12"
        android:ems="10"
        android:hint="Search query or URL"
        android:inputType="textUri"
        android:singleLine="true"/>

    <TextView
        android:id="@+id/error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>

EDIT 编辑

After Tomer Shemesh's suggestion, the <android.support.v7.widget.Toolbar was changed to: 在Tomer Shemesh的建议之后, <android.support.v7.widget.Toolbar更改为:

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

And then, the result is: 然后,结果是:

So there is no solution just yet. 因此,目前还没有解决方案。


Adding to Tomer suggestion, android:layout_marginTop="?attr/actionBarSize" should be applied to android.support.design.widget.NavigationView . 除了Tomer建议外, android:layout_marginTop="?attr/actionBarSize"应该应用于android.support.design.widget.NavigationView I dont see Navigation View in app_bar_main.xml . 我没有在app_bar_main.xml看到导航视图。

Adding this will give you action bar with a opaque overlay. 添加此选项将使操作栏具有不透明的叠加层。 It will look like this Image with opaque overlay 看起来像不透明覆盖的图像

You can remove opaque overlay by setting scrimColor to transparent of DrawerLayout in your Navigation Drawer Activity . 您可以通过将scrimColor设置为Navigation Drawer ActivityDrawerLayout透明来删除不透明的覆盖。

mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_refonte_base, null); mDrawerLayout.setScrimColor(ContextCompat.getColor(this,android.R.color.transparent));

Now it will look like this. 现在看起来像这样。 Image with scrim color as transparent 稀松布颜色为透明的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM