我有一个带有CollapsingToolbar和NestedScrollView的CoordinatorLayout。NestedScrollView的图像向上滚动(确定)。CollapsingToolbar有一个TabLayout(其中包含文本),当我滚动时,它会显示状态栏图标和文本,那不行,我想在进入状态栏之前隐藏TabLayout。有可能吗?
我有这个:
但是我想要这个:
如何在nestedScrollview内滚动内容,但隐藏Tabbar?
(更新)现在我有:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#505000FF"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="MY TAB"
android:textAlignment="center"
android:textColor="#FFF"
android:textSize="21sp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/content_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/tmb" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n
TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n
TEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \nTEST TEST \n"
android:textColor="#FFF" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在我的代码中,我使用以下方法调整边距:
private void applyTheme() {
getWindow().setStatusBarColor(Color.parseColor("#60000000"));
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.app_bar), (v, insets) -> {
((ViewGroup.MarginLayoutParams) topBar.getLayoutParams()).topMargin += insets.getSystemWindowInsetTop();
((ViewGroup.MarginLayoutParams) content.getLayoutParams()).topMargin = -((ViewGroup.MarginLayoutParams) topBar.getLayoutParams()).height - insets.getSystemWindowInsetTop();
return insets;
});
}
您可以在主样式中使用以下几个属性
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
UPDATE 1:
现在可以在ImageView
上方看到状态栏,
android:fitsSystemWindows="true"
中删除AppBarLayout
android:fitsSystemWindows="false"
添加到根视图CoordinatorLayout
因此,您的布局将是:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:fitsSystemWindows="false"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<RelativeLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#505000FF"
app:layout_scrollFlags="scroll|enterAlways">
....
这是测试时的演示
>
UPDATE 2
您的演示中的行为与我所需要的不同,因为“ 标签”可以从状态栏中看到,我需要在 状态栏,与netflix的主要活动相同。
您可以将
AppBarLayout.OnOffsetChangedListener
添加到AppBarLayout.OnOffsetChangedListener
并根据偏移值隐藏/显示AppBarLayout
。
我在下面的演示中使用了任意值(-10),但是您可以确定另一个小于0的值。
TextView