//透明状态栏效果
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
加入可实现透明栏
toolbar.inflateMenu(R.menu.menu);
加入菜单
menu 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"
tools:context="com.example.rx_android.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
编辑菜单
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
给标题设置回退键
setSupportActionBar(toolbar);
如果不需要用到actionbar的功能的花就不用写
<androidx.coordinatorlayout.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="wrap_content"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="180dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="#465456"
app:layout_scrollFlags="scroll"
app:expandedTitleTextAppearance="@style/transparentText"
app:collapsedTitleTextAppearance="@style/ToolbarTitle" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_head"
app:layout_collapseMode="parallax"
/>
<!--不能给toolbar中的background设置背景色否侧会遮住图片-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
app:layout_collapseMode="pin"
app:title="标题"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_arrow_back_black_24dp"
app:popupTheme="@style/ToolbarPopupTheme"
app:titleTextColor="@color/colorPrimary">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
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="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/large_text"
android:textSize="50dp" />
</LinearLayout>
```a
这里用到了4个布局 解释一下 第一个coordinatelayout是一个加强班的framelayout,也是具有覆盖的作用
但是他更适合交互 ,他有个layout—behave属性一般设置在appbar布局的外面,以便和toolbar交互
appbarlayout就是一个竖着的linerlayout ,他可以设置layout——scrollfalg就是说toolbar怎么滚动消失
用的比较多的是scroll和scroll|enteralways
CollapsingToolbarLayout就是折叠toolbar,可以设置更多的toolbar折叠属性,例如可以设置折叠是的颜色
app:contentScrim="#465456" 折叠方式 app:layout_collapseMode="pin" 或app:layout_collapseMode="parallax" pin表示折叠是固定 另一个就是就比较随意