首页 > 代码库 > 沉浸式状态栏

沉浸式状态栏

知识来源: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;

    }
}

 

沉浸式状态栏