android添加view遮层_DrawerLayout放在Toolbar的下方导致NavigationView出现与状态栏等高的遮罩层阴影...

目标是想让Toolbar不被DrawerLayout的NavigationView遮盖,一直显示。

以下代码运行于Android Studio 2.1.2, 手机5.1.1

1.按照Android Studio 模板Navigation Drawer Activity创建的项目:

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">

layout="@layout/content_main"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

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:showIn="@layout/activity_main"

tools:context="com.hooking.note.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:popupTheme="@style/AppTheme.PopupOverlay" />

android:id="@+id/fg_main_list"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

android:background="?attr/colorPrimary"

android:src="@drawable/ic_add_white_24dp"

app:layout_behavior="@string/floating_action_button_behavior" />

主题是android studio项目模板默认的

false

true

true

@android:color/transparent

android:name=".MainActivity"

android:theme="@style/AppTheme.NoActionBar" />

运行结果如下:

71b63306d307251c74362388d478d62e.png

2.修改代码,使抽屉里的布局在Toolbar下面

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="com.hooking.note.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:layout_scrollFlags="scroll|enterAlways"

app:popupTheme="@style/AppTheme.PopupOverlay" />

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

android:background="?attr/colorPrimary"

android:src="@drawable/ic_add_white_24dp"

app:layout_behavior="@string/floating_action_button_behavior" />

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="false"//注1

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:openDrawer="start">

android:id="@+id/fg_main_list"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

android:id="@+id/nav_view"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="start"

android:fitsSystemWindows="false"//注2

app:headerLayout="@layout/nav_header_main"

app:menu="@menu/activity_main_drawer" />

主题不变,运行结果如下:

63bc96d12c6c51ad3dc2960f783ef36e.png

发现NavigationView的顶部有一与状态栏高度相等的遮罩层,请问怎样去掉这部分遮罩层的阴影。

试过,修改DrawerLayout和NavigationView的布局属性;以及从以下代码涉及到颜色的函数着手修改,都没有效果

c9c0fc23dc35363f9b125354af008a1d.png

1f8491eef979df4dee409d10d45a437f.png

4ef7ed901fdff7b07b49bca0fd50859a.png

df3fe295f85d59524cbb1b3ee139f7fd.png

修改activity_main_2.xml、content_main_2.xml中DrawerLayout 【注1】 和NavigationView【注2】的 android:fitsSystemWindows="false"由false改为true,就出现了以下结果:可以看到内容区也多了状态栏同等高度的遮罩层阴影

82e9b72f6fa0e69daa88dc271db749d5.png

问题:请问,这个与状态栏等高的遮罩层阴影的来源是哪里,应该怎么去掉?谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以尝试在xml文件中将DrawerLayout组件和WebView组件的布局参数都设置为match_parent,并将WebView组件的android:layout_marginTop属性设置为AppBarLayout的度,这样WebView就会出现在AppBarLayout下方,而不会被DrawerLayout挡。具体代码示例如下: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <!-- 在AppBarLayout中添加Toolbar等其他组件 --> </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/app_bar_layout"> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize"/> </FrameLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="?android:attr/windowBackground" app:menu="@menu/bottom_navigation_menu" /> </RelativeLayout> <!-- 在DrawerLayout中添加侧滑菜单等其他组件 --> </androidx.drawerlayout.widget.DrawerLayout> ``` 这样设置后,WebView就可以正常滑动了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值