半透明/透明状态栏+ CoordinatorLayout +工具栏+片段
我有以下设置:
RecyclerView
Fragment
android:fitsSystemWindows="true"
问题是,在这种情况下,我无法获得状态栏的透明度。 我所做的是:
然后我尝试调整活动以编程方式获得如下所示的半透明工具栏:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void themeNavAndStatusBar(Activity activity)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
return;
Window w = activity.getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
w.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
w.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
w.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
}
将活动( @+id/frame_container
)中的占位符替换为片段
状态栏在这种情况下是纯色的,并且视图不在其下面绘制...为什么?
我想要的是
我需要一个工具栏,它滚动屏幕并完全隐藏,而此工具栏下方的内容应该适合屏幕,并在透明导航栏后面绘制。
布局
这是我的主要活动:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/clMain"
android:fitsSystemWindows="true"
android:background="?attr/main_background_color"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:fitsSystemWindows="true"
android:background="@null"
app:elevation="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="?actionBarThemeStyle"
app:popupTheme="?actionBarPopupThemeStyle"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivToolbarDataSource"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:layout_width="24dp"
android:layout_height="24dp" />
<TextView
android:id="@+id/tvToolbarTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:theme="?actionBarThemeStyle"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/tvToolbarSubTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:theme="?actionBarThemeStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- BUG: http://stackoverflow.com/questions/30541409/coordinatorlayoutappbarlayout-does-not-draw-toolbar-properly -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frame_container"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="32dp"
android:src="@drawable/ic_local_offer_white_24dp"
app:backgroundTint="?attr/colorPrimary"
app:borderWidth="0dp"
app:fabSize="normal"
app:rippleColor="?attr/colorPrimaryDark"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="com.test.classes.ScrollAwareFABBehavior"/>
</android.support.design.widget.CoordinatorLayout>
这是我的片段,将被放置在主要活动中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srlImages"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvImages"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<TextView
android:id="@+id/tvEmpty"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
编辑 - 截图
我使用浅色/深色底色主题和手工主题(因为用户可以选择任何颜色作为主要/重音颜色),所以不要介意工具栏是白色的(这是默认的主题背景色和原色)。 我还添加了黑色边框,以便您可以看到活动结束的位置。
最后,我会让工具栏和导航栏半透明以获得更好的视觉效果...
tl; dr将android:fitsSystemWindows="false"
至少设置为根CoordinatorLayout
以及内部碎片容器@frame_container
。
这可能不是最终的解决方案(即可能有其他fitsSystemWindows
更改),所以告诉我,如果你有任何问题。
为什么
当谈到状态栏,我想像这样fitsSystemWindows
:
fitsSystemWindows="false"
:由于您设置了窗口标志,因此通常会在状态栏下绘制视图。 fitsSystemWindows="true"
:通常在状态栏下绘制视图,因为您设置了窗口标志,但添加了顶部填充,以便内容在状态栏下方绘制并且不重叠。 事实上,在我看来,你看到的白色不是状态栏的颜色,而是你的CoordinatorLayout
背景。 这是由于协调器上的fitsSystemWindows="true"
:它将背景绘制到整个窗口,但向内容添加了顶部填充,因此内部视图不会被状态栏覆盖。
这不是你想要的。 您的内部内容必须由状态栏覆盖,因此您必须将fitsSystemWindows="false"
设置为协调器(因此它不会应用顶部填充),并且可能会将其设置为内容本身。
一旦你了解它的工作原理,就很容易调试并达到你正在寻找的效果。 其实并非如此。 几年过去了,但我仍然花费数小时的时间来找出正确的fitsSystemWindows组合,因为大多数视图(至少在支持库中)会覆盖上面所述的默认行为,这些行为通常不直观。 看到这篇文章的小指南如何使用它。
编辑您的styles.xml(v21),添加以下样式
<style name="AppTheme.Home" parent="AppTheme.Base">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
您可以根据自己的喜好更改父主题,但现在在您的AndroidManifest.xml文件中为此特定活动声明此主题:
<activity
android:theme="@style/AppTheme.Home"
android:name=".HomeActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait" />
这会让你的内容在透明的操作栏下可见。
现在使用以下内容将您的工具栏正确对齐到StatusBar下方,在您的oncreate中调用它:
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
使用以下内容获取状态栏高度:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
从协调器布局标记中删除以下内容:
android:fitsSystemWindows="true"
现在为了折叠您的工具栏或隐藏它,您可以参考以下教程:
http://antonioleiva.com/collapsing-toolbar-layout/
确保您使用的是以下版本的设计支持库,因为它是免费的:
compile 'com.android.support:design:23.1.0'
在阅读了关于您的问题的描述之后,我认为Google照片的风格符合您的要求。
好的,你的问题只有一些提示。 我的测试后,它的工作。
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
到你的样式中。 Toolbar
,并在内容向下滚动时平滑地显示Toolbar
,则需要根据当前代码将app:layout_collapseMode="parallax"
到Toolbar
的属性中。当然,您需要使用CollapsingToolbarLayout
CoordinatorLayout
和AppBarLayout
协调Toolbar
。 上一篇: Translucent/Transparent status bar + CoordinatorLayout + Toolbar + Fragment