让Android 5.0, 4.4 系统都实现沉淀式状态栏或透明状态栏的方法
高洛峰
高洛峰 2017-04-17 15:26:15
0
3
626

我想在5.0 或者4.4 操作系统都实现沉淀式状态栏或透明状态栏,最好全部都采用style 的方式实现,最好不用这个开源库https://github.com/jgilfelt/SystemBarTint求解决思路

经过这几天的思考和研究,发现使用ActionBar,和不使用ActionBar,改为Toolbar 效果完全是不一样的,那在这两种情况下是否都可以完美的实现呢

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
PHPzhong

对于top view, 可以用style,

<style name="TopViewStyle">
    <item name="android:fitsSystemWindows">true</item>
    <item name="android:clipToPadding">true</item>
</style>

但window flag还是需要代码做版本支持判断的,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

是没法在style中表示的,style自己做不了版本标识区别。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/top_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TopViewStyle"/>
    <TextView
        android:id="@+id/example_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

}
阿神

自己的解决方案:https://coding.net/u/tianshaokai/p/MaterialDesignDemo/git
低版本实现Material Design 的两种方式
https://github.com/Witype/SystemBarTintDemo
https://github.com/Witype/ToolbarStatusDemo

Peter_Zhu

实现状态栏沉浸非常简单,而且只需要通过style配置
values-v19 下的 styles.xml 里定义如下主题,并在 Manifest.xml 里将应用主题确定为这个主题即可。

<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

android:windowTranslucentStatus 表示状态栏透明
android:windowTranslucentNavigation 表示导航栏(虚拟按键栏)透明

沉浸是小事,关键麻烦在对 layout 布局里的改动,fitsSystemWindows 的优化,
凭经验告诉你,SystemBarTint已经算是最简单的解决方案。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!