如何实现android沉浸式状态栏
展开全部
沉浸式通知栏Android4.4以上才支持的新特性。4.3不支持。
具体实现方式如下:
1.新建个公共style,设置android:fitsSystemWindows=true
<!-- 设置应用布局时是否考虑系统窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,咱这个项目的activity样式都是统一的所以这样设置没问题,但是实际情况下不同的activity可能调用的样式不一样,需要自行按自己的项目来设置)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
3.新增沉浸式通知栏实现类,实现原理很简单。
1)判断当前系统版本是不是4.4以上,判断代码如下:
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
2)如果大于4.4则设置状态栏透明化,代码如下:
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3)获取activity的根rootView(DecorView),然后创建一个新的view stateBarView并把它添加到rootView(这里手动给它设置个ID,下次进来时先判断rootView是否已创建stateBarView,如果已创建则直接获取该View这样可以防止重复创建,导致内存泄露)
以下是具体代码实现:
import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知栏公共类 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //设置沉浸式通知栏的ID(防止重复创建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //获取状态栏高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加顶部状态栏 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //创建新的View,并添加到rootView顶部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}
具体实现方式如下:
1.新建个公共style,设置android:fitsSystemWindows=true
<!-- 设置应用布局时是否考虑系统窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,咱这个项目的activity样式都是统一的所以这样设置没问题,但是实际情况下不同的activity可能调用的样式不一样,需要自行按自己的项目来设置)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
3.新增沉浸式通知栏实现类,实现原理很简单。
1)判断当前系统版本是不是4.4以上,判断代码如下:
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
2)如果大于4.4则设置状态栏透明化,代码如下:
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3)获取activity的根rootView(DecorView),然后创建一个新的view stateBarView并把它添加到rootView(这里手动给它设置个ID,下次进来时先判断rootView是否已创建stateBarView,如果已创建则直接获取该View这样可以防止重复创建,导致内存泄露)
以下是具体代码实现:
import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知栏公共类 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //设置沉浸式通知栏的ID(防止重复创建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //获取状态栏高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加顶部状态栏 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //创建新的View,并添加到rootView顶部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
展开全部
沉浸式通知栏Android4.4以上才支持的新特性。4.3不支持。
具体实现方式如下:
1.新建个公共style,设置android:fitsSystemWindows=true
<!-- 设置应用布局时是否考虑系统窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,笔者项目的activity样式都是统一的所以这样设置没问题,但是实际情况下不同的activity可能调用的样式不一样,需要读者自行按自己的项目来设置)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
3.新增沉浸式通知栏实现类,实现原理很简单。
1)判断当前系统版本是不是4.4以上,判断代码如下:
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
2)如果大于4.4则设置状态栏透明化,代码如下:
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3)获取activity的根rootView(DecorView),然后创建一个新的view stateBarView并把它添加到rootView(这里手动给它设置个ID,下次进来时先判断rootView是否已创建stateBarView,如果已创建则直接获取该View这样可以防止重复创建,导致内存泄露)
以下是具体代码实现:
import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知栏公共类 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //设置沉浸式通知栏的ID(防止重复创建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //获取状态栏高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加顶部状态栏 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //创建新的View,并添加到rootView顶部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}
具体实现方式如下:
1.新建个公共style,设置android:fitsSystemWindows=true
<!-- 设置应用布局时是否考虑系统窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
2. 修改AndroidManifest.xml,让所有的activity样式默认设置为AppBaseTheme(*不同项目要灵活处理,笔者项目的activity样式都是统一的所以这样设置没问题,但是实际情况下不同的activity可能调用的样式不一样,需要读者自行按自己的项目来设置)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
3.新增沉浸式通知栏实现类,实现原理很简单。
1)判断当前系统版本是不是4.4以上,判断代码如下:
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
2)如果大于4.4则设置状态栏透明化,代码如下:
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3)获取activity的根rootView(DecorView),然后创建一个新的view stateBarView并把它添加到rootView(这里手动给它设置个ID,下次进来时先判断rootView是否已创建stateBarView,如果已创建则直接获取该View这样可以防止重复创建,导致内存泄露)
以下是具体代码实现:
import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知栏公共类 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //设置沉浸式通知栏的ID(防止重复创建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //获取状态栏高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加顶部状态栏 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //创建新的View,并添加到rootView顶部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 设置状态栏颜色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的顶级布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化状态栏 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //获取状态栏目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-04-17 · 知道合伙人软件行家
关注
展开全部
Android沉浸式状态栏实现(半透明浮动状态栏)Activity全屏显示
http://blog.csdn.net/ahuyangdong/article/details/51727559
http://blog.csdn.net/ahuyangdong/article/details/51727559
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |