沉浸式状态栏

Posted 岑忠满

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了沉浸式状态栏相关的知识,希望对你有一定的参考价值。

知识来源:https://zhidao.baidu.com/question/1989575143165411427.html

  (1)内容应用到状态栏(360流量允许拖动到状态栏的效果)

Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

  (2)在xml中设置一个带背景的与状态栏同高的空白

总代码如下

package com.cenzhongman.myapplication.Note;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.cenzhongman.myapplication.R;

import java.lang.reflect.Field;

public class NoteActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_layout);

        if (android.os.Build.VERSION.SDK_INT > 18) {
        /*
        *    API需要大于18
        *    隐藏状态栏
        *    让内容置于屏幕顶部
        *  1.如需隔开,在xml中添加android:fitsSystemWindows="true"
        *  2.添加一个状态栏高度的空白(此次使用方法2)
        * */
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        /*
        * 设置空白处高度
        *
        *
        * */
            int StatusBarHeight = getStatusBarHeight();
            Toast.makeText(this, StatusBarHeight + "123", Toast.LENGTH_SHORT).show();
            TextView status_bar = (TextView) findViewById(R.id.status_bar);
            status_bar.setHeight(StatusBarHeight);
        }
    }

    /*
    * 获取状态栏高度
    * 返回值,int 高度
    */
    public int getStatusBarHeight() {

        Class c = null;

        Object obj = null;

        Field field = null;

        int x = 0, statusBarHeight = 0;

        try {

            c = Class.forName("com.android.internal.R$dimen");

            obj = c.newInstance();

            field = c.getField("status_bar_height");

            x = Integer.parseInt(field.get(obj).toString());

            statusBarHeight = getResources().getDimensionPixelSize(x);

        } catch (Exception e1) {

            e1.printStackTrace();
        }

        return statusBarHeight;

    }
}

 

以上是关于沉浸式状态栏的主要内容,如果未能解决你的问题,请参考以下文章

Android沉浸式状态栏工具类,一句代码轻松搞定

沉浸式状态栏的最佳实现

沉浸式状态栏

Android 沉浸式状态栏

安卓沉浸式状态栏

Android 沉浸式状态栏