Android 11(Android R)状态栏高度获取

以前公共库里的代码,发现是使用如下代码获取状态栏高度的:

/** 
* 获取状态栏高度
* */  
int statusBarH = 0;  
try {  
    Class<?> clazz = Class.forName("com.android.internal.R$dimen");  
    Object object = clazz.newInstance();  
    int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());  
    statusBarH = getResources().getDimensionPixelSize(height);  
} catch (Exception e) {  
    e.printStackTrace();  
}  
return statusBarH;

其实从 Android 9 开始,就已经对通过反射调用非公开 API 的方式进行警告了。

Android 11以前的系统,只是给出警告, Android 11直接抛出了调用异常。

于是搜索一下,发现网上已经更改成如下方法了:

方案一:

int statusBarH = 0;  
//获取status_bar_height资源的ID  
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");  
if (resourceId > 0) {  
    //根据资源ID获取响应的尺寸值  
    statusBarH = getResources().getDimensionPixelSize(resourceId);  
}  
return statusBarH;

仔细观察两个方法,会发现,其实两者都是获取了系统里的状态栏使用的某个资源的高度信息,然后作为状态栏的高度信息

另外,网上流传的另一段代码

方案二:

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;

方案三:

WindowMetrics是Android
11新增的类,用于获取窗口边界,同样可以用来获取导航栏高度。

public static int getStatusBarHeight(Context context) {

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        WindowMetrics windowMetrics = wm.getCurrentWindowMetrics();
        WindowInsets windowInsets = windowMetrics.getWindowInsets();
        Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());
        return insets.top;
    }       
    
    ....
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android系统从5.0开始引入了Material Design风格,其中一个重要的改变是状态栏的颜色和样式。在之前的版本中,状态栏通常是黑色的,而在Material Design中,状态栏可以根据应用程序的主题颜色进行定制化。因此,我们需要在不同的Android版本上适配状态栏。 在Android 5.0及以上版本中,我们可以使用以下代码来实现状态栏颜色的定制: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(ContextCompat.getColor(this, R.color.status_bar_color)); } ``` 在Android 4.4及以下版本中,我们可以通过设置一个透明的状态栏来模拟状态栏颜色: ```java if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) window.getDecorView(); int statusBarHeight = getStatusBarHeight(); View statusBarView = new View(this); statusBarView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight)); statusBarView.setBackgroundColor(ContextCompat.getColor(this, R.color.status_bar_color)); decorView.addView(statusBarView); ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0); rootView.setFitsSystemWindows(true); rootView.setClipToPadding(true); } ``` 其中,`getStatusBarHeight()`方法可以获取状态栏高度。 在Android 6.0及以上版本中,状态栏的颜色还可以与应用程序的主题颜色进行匹配。我们可以在`styles.xml`中定义一个`colorPrimaryDark`属性来指定状态栏颜色: ```xml <item name="colorPrimaryDark">@color/status_bar_color</item> ``` 在代码中,我们可以通过以下方式来设置主题: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值