實(shí)現(xiàn)沉浸式狀態(tài)欄,意思是可以activity狀態(tài)欄的內(nèi)容可以顯示在狀態(tài)欄。
相關(guān)控件都來自于Design support library;是屬于Material Design的思想,包括CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、Toolbar,通過這幾個就可以完美的實(shí)現(xiàn)。
注意要點(diǎn):
1.新建values-19、values-21文件夾及styles.xml文件
- values/styles
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.No" parent="AppTheme" />
- values-19、values-21/styles:
<style name="AppTheme.No" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
接下來就是activity的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/img_collapsed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/time"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/tv_scroll_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
activity.java相關(guān)代碼:
private void initView() {
toolbar = findViewById(R.id.toolbar);
collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout);
imgCollapsed = findViewById(R.id.img_collapsed);
toolbar.setTitle("AppBarLayout");
toolbar.setNavigationIcon(R.drawable.btn_24_back_b_nor);
collapsingToolbarLayout.setTitle("CollapsingToolbar");
collapsingToolbarLayout.setContentScrimColor(ContextCompat.getColor(this, R.color.colorAccent));
collapsingToolbarLayout.setStatusBarScrimColor(ContextCompat.getColor(this, R.color.colorAccent));
setSupportActionBar(toolbar);
// setImmerseLayout(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
tvScrollContent = findViewById(R.id.tv_scroll_content);
//獲取StatusBar高度,并設(shè)置給標(biāo)題欄(Toolbar)
protected void setImmerseLayout(View view) {// view為標(biāo)題欄
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight = getStatusBarHeight(this.getBaseContext());
view.setPadding(0, statusBarHeight, 0, 0);
}
}
public int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
"android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
看了幾篇相關(guān)的博文,然后自己親自試著寫了一下,遇到了一些問題,所以還是要實(shí)踐一下。但還是有些模糊。有時間再理解一下。