在画UI过程中,有些情况下需要知道statusBar高度:
网上有些方法是这样的:
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.v("@@@@@@", "the statusbar Height is : " + statusBarHeight);
类似:
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight;
Log.v("@@@@@@", "the titleBar Height is : " + titleBarHeight);
或者:
Rect rect = new Rect();
MainActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
View view = MainActivity.this.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int topS = rect.top;//状态栏高度
int topT = rect.height() - view.getHeight();
Log.v("@@@@@@", "the statusbar Height is : " + topS);
Log.v("@@@@@@", "the titleBar Height is : " + topT);
在我的手机上,均不行。
后:
public static int getStatusBarHeight(Context context){
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 = context.getResources().getDimensionPixelSize(x);
Log.v("@@@@@@", "the status bar height is : " + statusBarHeight);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
经测试可行。
分享到:
相关推荐
主要介绍了Android有效获取状态栏(StatusBar)高度的方法,涉及Android针对状态栏(StatusBar)属性操作的相关技巧,需要的朋友可以参考下
安装 ...rootProject.projectDir,"../node_modules/react-native-statusbar-props/android") build.gradle dependencies { compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.a
android 平板的action bar、status bar、navigation bar的高度
React本机状态栏高度小型图书馆,可帮助您获取状态栏的高度聚苯乙烯 :mobile_phone: 支持X :red_heart:安装$ npm install --save react-native-status-bar-height# OR$ yarn add react-native-status-bar-height用法...
主要参考stackoverflow的这篇问答:http://stackoverflow.com/questions/3407256/height-of-status-bar-in-android 方法一: private double getStatusBarHeight(Context context){ double statusBarHeight = Math...
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } 3,...
Android Compose样本存储库一个Android应用程序,提供Android Jetpack Compose的...SideMenuItem / StatusBar /屏幕状态不要忘了给存储库加注星标,并等待更多示例:D 讨论区请参阅问题部分: https : //github.com/
android-statusbar-library 状态栏库,兼容5.0版本以下,不需要设置主题样式即可修改状态栏颜色,使用起来很方便 API说明:CoreStatusBar类,主要是用与修改状态栏颜色,获取状态栏和导航栏高度 public static void ...
{ int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); try { return getResources().getDimensionPixelSize(resourceId); } catch (Resources....
Android 键盘/Android 系统/API Domes/Certificate Installer(证书安装)/com.android.gestuer.builder()/com.android.sdksetup()/Custom Locale()/Dev Tools(开发工具)/Example Wallpapers(墙纸式样)/HTML查看器/...
vivo-X21A: screenRealSize : 2280 , screenHeight : 2201, statusBar : 84, navigationBar : 126(Android9导航栏一键) vivo-X21A: screenRealSize : 2280 , screenHeight : 2075, statusBar : 84, navigationBar ...