2023年6月27日发(作者:)
Android刘海、挖孔、⽔滴屏幕的状态⾼度获取与适配⼀般需要处理状态栏的问题都是适配问题,适配就稍微有点⿇烦,⽐如:刘海屏,⽔滴屏、以及刚刚不久出的挖孔屏等这类都属于刘海屏范围内,适配的话就要在各⼤⼚商做处理了,⽐如华为:刚刚开始以为只需判断是否是刘海屏就好了,设置⼀个状态栏⾼度就好Ok了,结果不然:状态栏⾼度: private fun setStatusHeight() { val layoutParams = base_Params as Params gins(0,tusBarHeight(mContext),0,0) base_Params = layoutParams }/** * 获得通知栏⾼度 */ public static int getStatusBarHeight(Context context) { Class> c = null; Object obj = null; Field field = null; int x = 0, statusBarHeight = 0; try { c = e("al.R$dimen"); obj = tance(); field = ld("status_bar_height"); x = nt((obj).toString()); statusBarHeight = ources().getDimensionPixelSize(x); } catch (Exception e1) { tackTrace(); } return statusBarHeight; }因为这⾥设置的是铺满全屏,需要重新计算状态栏⾼度,在⼩⽶、oppo、华为的刘海屏和⽔滴屏幕没有问题,但在华为的挖孔屏上出现问题,就是常见的重叠,其他品牌的⼿机因为没有,不知道有没有出现类似的问题,先说问题原因是:华为的状态栏⾼度是获取到了的值很⼩,只有26仔细看到话在挖孔的上⾯很⼩的空⽩处是有⾼度的那个就是状态栏⾼度,导致出现重叠的原因,解决⽅法就是获取挖孔的⾼度,把值设置进去就好了; /** * 获取华为刘海的⾼ * * @param context * @return */ public static int getNotchSizeAtHuawei(Context context) { int[] ret = new int[]{0, 0}; try { ClassLoader cl = ssLoader(); Class> HwNotchSizeUtil = ass("hSizeUtil"); Method get = hod("getNotchSize"); ret = (int[]) (HwNotchSizeUtil); } catch (ClassNotFoundException e) { Log.e("NotchScreenUtil", "getNotchSize ClassNotFoundException"); } catch (NoSuchMethodException e) { Log.e("NotchScreenUtil", "getNotchSize NoSuchMethodException"); } catch (Exception e) { Log.e("NotchScreenUtil", "getNotchSize Exception"); } return ret[1]; }这个是专门针对华为过去的⾼度,其他品牌获取的值为0,现在只适配了这些后期有问题会补上。/** * 是否有刘海屏 * * @return */ public static boolean hasNotchInScreen(Activity activity) { // android P 以上有标准 API 来判断是否有刘海屏 if (_INT >= N_CODES.P) { View decorView = dow().getDecorView(); WindowInsets windowInsets = tWindowInsets(); if (windowInsets != null) { DisplayCutout displayCutout = playCutout(); if (displayCutout != null) { List
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687840117a49833.html
评论列表(0条)