参考资料:http://blog.cgsdream.org/2017/03/16/android-translcent-statusbar/
https://blog.csdn.net/coderder/article/details/78294777
开发过程中,首页顶部是一张图片,要求占据全屏幕,状态栏覆盖在图片之上。
首先,activity的theme为:
<style name="TestAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">#4D0F294D</item>
<item name="colorPrimary">@color/color_main</item>
<item name="android:windowBackground">@color/white</item>
</style>
colorPrimaryDark 表示状态栏背景颜色。
怎么实现沉浸式状态栏:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
//这里只对api21以上做处理
如果想实现MD,页面上滑,toolbar显示出来,并且statusbar逐渐不透明。
需要给statusbar一个占位view:
activity的XML文件为:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/background_iv"
android:layout_width="match_parent"
android:layout_height="138dp"
android:scaleType="centerCrop"
android:src="@drawable/train_pic_brand" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="24dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
app:layout_collapseMode="pin">
<com.ctrip.ibu.train.widget.TrainI18nTextView
style="@style/TextAppearance.Trip.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Train"
android:textColor="@color/color_white"
android:textAppearance="@style/TextAppearance.Trip.Medium"
android:textSize="28dp" />
<View
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_marginBottom="7dp"
android:layout_marginStart="4dp"
android:layout_gravity="bottom"
android:background="@drawable/train_circle_ff9500" />
</LinearLayout>
//占位statusbar
<View
android:id="@+id/status_bar"
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="@drawable/train_bg_ffffff"
app:layout_collapseMode="pin" />
<com.ctrip.ibu.train.widget.TrainNewToolbar
android:id="@+id/train_tool_bar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:layout_marginTop="24dp"
android:background="@drawable/train_bg_ffffff"
app:layout_collapseMode="pin" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_12dp"
android:layout_gravity="bottom"
android:background="@drawable/train_bg_main_search_view"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/color_page_bg"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<LinearLayout
android:id="@+id/ll_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
java代码中:
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mToolbar.setTitleColor(R.color.color_train_main_text);
int totalScrollRange = appBarLayout.getTotalScrollRange();
float offset = (-verticalOffset) / (float) totalScrollRange;
//toolbar变化
mStatusBar.setAlpha(offset); //statusbar透明度变化
mToolbar.getBackground().setAlpha((int) (offset * 255)); //toolbar透明度变化
mToolbar.getTitleTv().setAlpha(offset);
});