上一篇文章我们已经写好了,而且效果也不错,不过水果的背景图片和状态栏没有融合在一起,融合在一起效果就更好了。
Android5.0系统之后支持对状态栏和背景进行操作(使背景图和状态栏融合)。
我们需要借助android:fitsSystemWindows这个属性来完成。我们需要在ImageView里设置这个属性,以及imageView嵌套的所有父布局(CoordinatorLayout,AppBarLayout,CollapingToobarLayout这种嵌套结构的布局中将这个属性设置为true(表示该控件会出现在系统状态栏里))都要添加这个属性。
修改activity_fruit.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/fruit_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
...
</android.support.design.widget.CoordinatorLayout>
定义一个名为FruitActivityTheme的主题,parent是AppTheme,意思是继承了AppTheme所有的特性,然后在android:statusBarColor里将状态栏指定成透明色。【values-21文件 只有5.0及以上的系统高才能会读取】
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="FruitActivityTheme" parent="AppTheme">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
5.0系统之前无法识别FruitActivityTheme这个主题,我们在values里的styles.xml添加一些代码
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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="FruitActivityTheme" parent="AppTheme"></style>
</resources>
修改AndroidManifest.xml,让FruitActivity使用FruitActivityTheme主题(只需要添加一行代码)
<activity android:name=".FruitActivity"
android:theme="@style/FruitActivityTheme"></activity>
运行效果如图
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务