gpt4 book ai didi

Android - 工具栏在 ListView 滚动时高于状态栏

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:45 26 4
gpt4 key购买 nike

我试图在滚动 ListView 时使用协调器布局隐藏工具栏。我在我的工具栏上使用了 app:layout_scrollFlags="scroll|enterAlways",在我的 ListView 中使用了 android:nestedScrollingEnabled="true"

当我向下滚动 ListView 时,工具栏正确隐藏,但它位于状态栏“上方”。在网络开发术语中,看起来工具栏的“Z-Index”高于状态栏。这是它的样子-

enter image description here Toolbar above Statusbar

这是我的activity_main.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:context=".MainActivity"
android:background="#FFFFFF" >

<android.support.design.widget.AppBarLayout android:id="@+id/appbar"
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="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways" />

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

<android.support.v4.view.ViewPager android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

这是我的fragment_main.xml代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollingCache="false"
android:nestedScrollingEnabled="true" />

</RelativeLayout>

如何解决这个问题?提前致谢!

更新:我已经在下面发布了我的答案!检查已接受的答案。

最佳答案

我终于解决了这个问题。如果其他人卡在这里,这就是我修复它的方式。无需将您的布局更改为 Recyclerview 和所有!以下是步骤。

1) 将 CoordinatorLayout 的 fitsSystemWindows 设置为 false。

android:fitsSystemWindows="false"

2) 现在,我的状态栏变成了白色。要解决此问题,请将以下行添加到 MainActivity.java 中的 onCreate 方法

Window window = getWindow();

// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));

瞧!现在一切正常。

这是我的 activity_main.xml 内容,以防我遗漏了什么。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:contentScrim="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll|enterAlways">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways" />

<android.support.design.widget.TabLayout android:id="@+id/tabs"
android:layout_width="match_parent" android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="3dp"
android:textStyle="bold"/>

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

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

关于Android - 工具栏在 ListView 滚动时高于状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com