【问题标题】:Navigation Drawer Layout overlapping with frame layout导航抽屉布局与框架布局重叠
【发布时间】:2016-10-27 19:04:26
【问题描述】:

每当我打开导航抽屉框架布局内容时,我什至尝试将框架的颜色设置为白色

ma​​in_activity.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout  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:id="@+id/drawer_layout"
tools:context="com.example.sumanravi.knowmycity.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content_layout"
        android:background="@color/white">
    </FrameLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical">
    <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/navList"
        android:choiceMode="singleChoice">
    </ListView>
</LinearLayout>

【问题讨论】:

  • background 颜色设置为白色仍将显示任何视图的内容。如果要隐藏视图的内容,应使用View#setVisibility(int) 将其可见性设置为View.GONEView.VISIBLE
  • 这很好用,但是无论何时打开或关闭抽屉,都需要一些时间才能使内容可见和不可见,请帮我解决这个问题
  • 好吧,我现在就完整回答:)
  • 顺便说一句,如果它对您有用,您应该接受答案,以便我们可以关闭问题。

标签: android navigation-drawer


【解决方案1】:

如果您想要隐藏布局的内容,您可以:

  1. 隐藏整个布局(将可见性设置为View.GONEView.VISIBLE
  2. 更改 alpha(在布局本身上设置 alpha)
  3. 删除所有布局的子项(请不要使用此方法,它非常慢)

包含视图将始终在其下方绘制其父级的背景。隐藏内容的另一种有趣的方法是使用抽屉监听器:https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html#onDrawerSlide(android.view.View, float) - 查找 onDrawerSlide(View, float) 方法并(例如)随时更改视图的 alpha。

您可以使用 FrameLayouts 做的另一件事是设置前景:set forground color in framelayout in android programatically

但通常,导航抽屉下的内容始终(至少部分)可见。如果你想隐藏它,你可能没有关注the guide

【讨论】: