Android手机状态栏

一、全屏不保留状态栏(欢迎页):

在style.xml中设置


    <style name="Theme.AppStartLoadTranslucent" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

在Activity中设置:

public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
//全屏展示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Timer timer = new Timer();// 实例化Timer类
        timer.schedule(new TimerTask() {
            public void run() {
                Intent intent = new Intent(WelcomeActivity.this,
                        MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);// 五百毫秒
    }

}

去掉欢迎页中白色闪屏的方法:

解决闪屏只需要设置一个透明的Theme,在styles.xml添加如下代码

<style name="WelcomeTheme" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
    </style>

在minifist.xml中设置:


        <activity
            android:name=".view.login.WelcomeActivity"
            android:alwaysRetainTaskState="true"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppStartLoadTranslucent">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

二、 全屏保留状态栏文字(页面上部有Banner图)

Window window = getWindow();
//默认API 最低19 
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    ViewGroup contentView = window.getDecorView().findViewById(Window.ID_ANDROID_CONTENT);
    contentView.getChildAt(0).setFitsSystemWindows(false);
}

三、 标题栏与状态栏颜色一致

xml中配置:

<style name="status_toolbar_same_color" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/status_toolBar_same_color</item>
    <item name="colorPrimaryDark">@color/status_toolBar_same_color</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

如果在低于21版本手机上 ,设置如下:

Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.status_toolBar_same_color));
} else {
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    ViewGroup systemContent = findViewById(android.R.id.content);
    View statusBarView = new View(this);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
    statusBarView.setBackgroundColor(getResources().getColor(R.color.status_toolBar_same_color));
    systemContent.getChildAt(0).setFitsSystemWindows(true);
    systemContent.addView(statusBarView, 0, lp);
}

四、状态栏文字颜色设置

//设置白底黑字
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView()
        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

除此国内厂商小米、魅族也开放了修改状态栏字体的方式:

  • 小米 MIUI6

    https://dev.mi.com/doc/p=4769/index.html

  • 魅族 Flyme

    http://open-wiki.flyme.cn/index.php?title=状态栏变色

猜你喜欢

转载自my.oschina.net/u/928906/blog/1633202