Android 中沉浸式状态栏实现方式如下

计算状态栏高度及调用沉浸式状态栏的相关API方法

 package com.example.status;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class ImmersedStatusbarUtils { /**
* 在{@link Activity#setContentView}之后调用
*
* @param activity
* 要实现的沉浸式状态栏的Activity
* @param titleViewGroup
* 头部控件的ViewGroup,若为null,整个界面将和状态栏重叠
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void initAfterSetContentView(Activity activity,
View titleViewGroup) {
if (activity == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
if (titleViewGroup == null)
return;
// 设置头部控件ViewGroup的PaddingTop,防止界面与状态栏重叠
int statusBarHeight = getStatusBarHeight(activity);
titleViewGroup.setPadding(0, statusBarHeight, 0, 0); } /**
* 获取状态栏高度
*
* @param context
* @return
*/
private static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}

调用实例,使用步骤一编写的相关方法。

 package com.example.status;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
View topView = findViewById(R.id.lin);
ImmersedStatusbarUtils.initAfterSetContentView(this, topView);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

对应的布局文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff123654" > <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="45.0dp"
android:background="#ff123654"
android:gravity="center"
android:text="我是头部控件"
android:textColor="#ffffffff" />
</LinearLayout> </LinearLayout>

Android 中沉浸式状态栏实现的更多相关文章

  1. Android中沉浸式状态栏的应用

    在Android5.0版本后,谷歌公司为Android系统加入了很多新特性,刷新了Android用户的体验度.而其中的一个新特性就是沉浸式状态栏.那么问题来了,很多非移动端的小伙伴就要问了,什么是沉浸 ...

  2. 81.Android之沉浸式状态栏攻略

    转载:http://blog.csdn.net/lmj623565791/article/details/48649563/ 一.概述 近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下:本篇博客 ...

  3. Android App 沉浸式状态栏解决方案

    伴随着 Android 5.0 发布的 Material Design,让 Android 应用告别了以前的工程师审美,迎来了全新的界面,灵动的交互,也让越来越多的 App 开始遵从 material ...

  4. Android 之 沉浸式状态栏及顶部状态栏背景色设置

    现在很多应用都引用了沉浸式状态栏,如QQ,效果下图: 效果很酷炫,其实设置也很简单.但是,需要注意的是,这种效果只能在API19以及以上版本中才能够做到. 方法一: 首先,如果想让界面Activity ...

  5. 【Android】沉浸式状态栏实现

    在Android4.4(API 19)及以后的版本中都增加了对沉浸式状态栏的支持,实现起来也很简单,将application的主题稍作修改即可: <style name="AppThe ...

  6. Android:沉浸式状态栏(一)工具类

    参考自Android 沉浸式状态栏完美解决方案 基本功能 状态栏深色或浅色图标切换 自定义状态栏背景 设置沉浸式状态栏 先准备几个工具类 1.SystemBarTintManager package ...

  7. Android实现沉浸式状态栏(必须高逼格)

     情况一:保留状态栏 只是将我们的布局嵌入到状态栏中 方法一:通过设置theme主题 因为 API21 之后(也就是 android 5.0 之后)的状态栏,会默认覆盖一层半透明遮罩.且为了保持4.4 ...

  8. Android:沉浸式状态栏(二)集成

    在Activity的onCreate()方法中添加代码 //设置状态栏透明 StatusBarUtil.setTranslucentStatus(this); //设置透明状态栏的paddingTop ...

  9. Android 自动取色并设置沉浸式状态栏

    Android 自动取色并设置沉浸式状态栏 - Stars-One的杂货小窝 最近在进行产品的优化,也是研究了下沉浸式状态栏的实现方法及自动取色,记录一下笔记 设置沉浸式状态栏 1.添加依赖 这里,是 ...

随机推荐

  1. 一个简单的猜大小的小游戏 python

    初学python,用python写了一个简单的猜大小的小游戏 #!/usr/bin/env python #-*- coding:utf-8 -*- print "------------- ...

  2. [Linux] Chang DNS Setting on Linux

    主机的虚拟机使用 NAT 模式时, NAT的DNS不好用.于是需要将虚拟机的DNS改成和主机的一样,这样虚拟机也可以请求互联网资源. Linux的DNS 服务器定义在 /etc/resolv.conf

  3. HDOJ 2444 The Accomodation of Students

    染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  4. bootstrap-scrollspy

    功能:用在导航栏上,用于滚动内容时动态对应导航标签 插件:scrollspy.js 要点:scrollspy用在导航栏.导航条上居多.body添加data-spy="scroll" ...

  5. 01WebApi防篡改机制---HMAC机制

    防篡改,顾名思义就是防止有人恶意篡改请求数据URL以达到恶意攻击的目的,那要怎么才能实现这样的目的呢? 很简单,将要请求的数据加上合作号.合作Key按规则组织成一个字符串,获取对应的MD5摘要,然后将 ...

  6. 批处理快速更改ip地址

    在各种网络中切换,windows更换ip地址步骤:  进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...

  7. 迟来的Android的Camera开发总结

    这是好久前写的项目,但一直没有去总结.刚好在准备找工作这段时间来总结自己做过的东西,学到的东西. 写Android的自定义的相机应用时,首先要知道一些Camera开发必须知道的尺寸,不然在调试的时候, ...

  8. C++公有派生

    C++中的公有派生: 1.格式:Class A:public B{...}: 例如我们先声明了一个基类:SafeIntArray,当我们要声明它的派生类IndexIntArray时,格式如下:

  9. cocos2d事件处理机制之我见

    cocos2d是使用pyglet事件框架来处理事件的. 其中,包括分发器(发射器)和监听器两部分.下面形象的来打个比方. 这个机制就好比一把枪,三步:扣扳机(触发),上弹(注册),给子弹上***(这个 ...

  10. MySQL 同主机不同数据库之间的复制

    MySQL同主机不同数据库的复制命令:注意运行在Terminal中,不运行在MySQL命令行中. mysqldump Portal_DEV -u root -ppassword1 --add-drop ...