滚动时 AppBar 没有完全隐藏在状态栏中
AppBar not hiding completely in status bar when scrolling
我正在尝试使用 AppBar 布局实现搜索栏(如 GDrive 或 Keep Notes 中的搜索栏),因此当您向上滚动时它会从屏幕上消失,向下滚动时会出现。另外,我在 style.xml 文件中使状态栏透明。但是当我向上滚动时,AppBar 并没有完全隐藏,所以你可以在状态栏中看到它的底部。我怎样才能解决这个问题以将其完全隐藏在屏幕上?
Image
应用栏
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:background="@color/colorPrimary"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_47sdp"
app:elevation="0dp"
android:translationZ="0.1dp"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/_37sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginStart="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_15sdp"
android:background="@drawable/background_search"
app:layout_scrollFlags="scroll|enterAlways|snap" >
<ImageView
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginStart="@dimen/_5sdp"
android:src="@drawable/ic_search"
app:tint="@color/colorSearchIcon"
android:layout_marginEnd="@dimen/_3sdp"
android:contentDescription="@string/search_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/_20sdp"
android:layout_marginStart="@dimen/_12sdp"
android:layout_marginTop="@dimen/_10sdp"
android:fontFamily="@font/opensans_regular"
android:includeFontPadding="false"
android:gravity="center_vertical"
android:text="@string/search"
android:textColor="@color/colorWhite"
android:textSize="@dimen/_13ssp"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
我的问题已通过 so ():
以编程方式解决
// In Activity's onCreate() for instance
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
我正在尝试使用 AppBar 布局实现搜索栏(如 GDrive 或 Keep Notes 中的搜索栏),因此当您向上滚动时它会从屏幕上消失,向下滚动时会出现。另外,我在 style.xml 文件中使状态栏透明。但是当我向上滚动时,AppBar 并没有完全隐藏,所以你可以在状态栏中看到它的底部。我怎样才能解决这个问题以将其完全隐藏在屏幕上?
Image
应用栏
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:background="@color/colorPrimary"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_47sdp"
app:elevation="0dp"
android:translationZ="0.1dp"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/_37sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginStart="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_15sdp"
android:background="@drawable/background_search"
app:layout_scrollFlags="scroll|enterAlways|snap" >
<ImageView
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginStart="@dimen/_5sdp"
android:src="@drawable/ic_search"
app:tint="@color/colorSearchIcon"
android:layout_marginEnd="@dimen/_3sdp"
android:contentDescription="@string/search_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/_20sdp"
android:layout_marginStart="@dimen/_12sdp"
android:layout_marginTop="@dimen/_10sdp"
android:fontFamily="@font/opensans_regular"
android:includeFontPadding="false"
android:gravity="center_vertical"
android:text="@string/search"
android:textColor="@color/colorWhite"
android:textSize="@dimen/_13ssp"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
我的问题已通过 so (
// In Activity's onCreate() for instance
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}