【问题标题】:Android - Prevent navigation drawer from overlaying title barAndroid - 防止导航抽屉覆盖标题栏
【发布时间】:2018-02-20 13:30:03
【问题描述】:

我最近在 Android Studio 中打开了一个项目并选择了一个 Navigation Drawer Activity。它创建了导航抽屉,只是它以我不喜欢的方式显示 - 导航抽屉显示在标题栏的顶部,例如:

相反,我希望它是这样的:

在这个“正确”示例中,标题栏 - “主页”与关闭箭头一起显示,同时导航抽屉打开。

文件来源:

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>

编辑

在 Tomer Shemesh 的建议下,&lt;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"/>

然后,结果是:

所以目前还没有解决方案。

【问题讨论】:

标签: android user-interface kotlin navigation-drawer


【解决方案1】:

除了 Tomer 的建议之外,android:layout_marginTop="?attr/actionBarSize" 应该应用于 android.support.design.widget.NavigationView。我在app_bar_main.xml 中看不到导航视图。

添加此项将为您提供带有不透明叠加层的操作栏。它看起来像这样 Image with opaque overlay

您可以通过在Navigation Drawer Activity 中将scrimColor 设置为DrawerLayout 的透明来移除不透明的覆盖。

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

现在它看起来像这样。 Image with scrim color as transparent

【讨论】:

  • 没有android.support.design.widget.NavigationView 因为Android Studio 出于某种原因没有生成它。 &lt;RelativeLayout ... 最初不存在 - 有 &lt;include ...&gt; 但我更喜欢将所有代码放在同一个地方,所以我只是将引用的代码放在同一个 XML 文件中。
  • 哦等等。刚刚找到&lt;android.support.design.widget.NavigationView ... - 它位于activity_main.xml。试试看
  • 经过测试。 ALMOST 有效(尚未应用透明度)。
  • avi - 这行得通吗?你能接受这个解决方案吗?
  • 遗憾的是,我最终放弃了导航抽屉,转而使用底部选项卡,到目前为止,我正在处理代码而不是 UI
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多