沉浸式(图片内容延伸状态栏)
2018-08-06 本文已影响0人
jjswer
ezgif.com-video-to-gif.gif
设置系统UI参数和修改Toolbar
private fun fullScreen(activity: Activity) {
//设置系统UI参数
//setSystemUiVisibility(int visibility)传入的实参类型如下:
//1.View.SYSTEM_UI_FLAG_VISIBLE :状态栏和Activity共存,Activity不全屏显示。也就是应用平常的显示画面
//2.View.SYSTEM_UI_FLAG_FULLSCREEN :Activity全屏显示,且状态栏被覆盖掉
//3. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN :Activity全屏显示,但是状态栏不会被覆盖掉,而是正常显示,只是Activity顶端布 局会被覆盖住
//4.View.INVISIBLE : Activity全屏显示,隐藏状态栏
//5.View.SYSTEM_UI_FLAG_LAYOUT_STABLE : 稳定布局
//6.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR :改变状态栏字体颜色 (android 6.0以上有效)
val window: Window = activity.window
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
val decorView: View = window.decorView
val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
decorView.systemUiVisibility = option
//给系统状态栏着色:
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//给系统状态栏设置透明颜色:
window.statusBarColor = Color.TRANSPARENT
//获取Toolbar控件,获取状态栏高度,给Toolbar控件的MarginTop设置状态栏的高度
//Toolbar父控件为CollapsingToolbarLayout
val layoutParams: CollapsingToolbarLayout.LayoutParams = coordinator_toolbar.layoutParams as CollapsingToolbarLayout.LayoutParams
layoutParams.setMargins(0, getStatusBarHeight(), 0, 0)
coordinator_toolbar.layoutParams = layoutParams
}
获取状态栏高度
fun getStatusBarHeight(): Int {
var result = 0
val resourceId = resources.getIdentifier("status_bar_height", "dimen", " ")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}
布局:
<android.support.design.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"
tools:context=".ui.index.PhotoAlbumDetailActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleTextAppearance="@style/AlbumCollapsedTitle"
app:contentScrim="@color/white"
app:expandedTitleTextAppearance="@style/AlbumExpandedTitle"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_top_bg"
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="centerCrop"
android:src="@drawable/default_square_image_1" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/coordinator_toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextAppearance="@style/Toolbar.TitleText" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/margin_small"
android:paddingRight="@dimen/margin_small"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>