首页 > 代码库 > 沉浸式状态栏
沉浸式状态栏
知识来源: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; } }
沉浸式状态栏
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。