完美获取Android状态栏高度

本文分享了一个有效获取Android状态栏高度的代码实例,适用于Android后期版本应用开发,解决无法直接退出的问题。通过使用反射获取内部类的字段值,实现获取状态栏高度的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来自: http://www.cnblogs.com/LuoYer/archive/2011/11/06/2238167.html

闲暇写了个单本小说阅读的应用。中间碰到了需要获取状态栏高度的问题。

就像android后期版本,无法直接退出一样。找了一些方法来获取状态栏高度,结果都是为0.

还好,牛人是很多的,当时,找到一段代码,能够有效的获取状态栏的高度。特此记录,备忘,以及供大家参考。

Class<?> c =  null ;
Object obj =  null ;
Field field =  null ;
int  x =  0 , sbar =  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());
     sbar = getResources().getDimensionPixelSize(x);
catch (Exception e1) {
     loge( "get status bar height fail" );
     e1.printStackTrace();
}

个人注:以下代码不能在onCreate里面使用,否则获取状态栏高度为0

Rect frame =  new Rect();

getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

### Android 沉浸式状态栏实现方案与适配指南 #### 1. 沉浸式状态栏的概念 沉浸式状态栏设计旨在通过调整状态栏、导航栏以及其他系统UI组件的显示效果,减少其对应用界面的影响,从而提供更佳的用户体验[^2]。这种技术的核心在于使应用程序能够充分利用屏幕空间,同时保持系统的可操作性和信息可见性。 #### 2. 基础实现方法 为了实现沉浸式状态栏,通常需要以下几个步骤: - **设置主题** 配置`styles.xml`文件中的主题属性,例如将状态栏设为透明: ```xml <style name="AppTheme.NoActionBar"> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:windowTranslucentStatus">true</item> </style> ``` - **自定义布局** 在布局文件中合理配置`fitsSystemWindows`属性。如果需要让工具栏占据状态栏位置,则应将其应用于`Toolbar`而非根布局[^4]: ```xml <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:fitsSystemWindows="true"/> ``` - **动态控制状态栏颜色** 可以通过编程方式改变状态栏的颜色或图标亮度模式: ```java Window window = getWindow(); window.setStatusBarColor(ContextCompat.getColor(this, R.color.new_status_bar_color)); View decorView = window.getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); ``` #### 3. 特殊场景下的适配策略 在某些特殊情况下(如刘海屏、折叠屏),需额外考虑避让区域的处理[^5]: - **获取挖孔区域信息** 利用`DisplayCutout`类检测屏幕是否存在缺口并相应调整布局: ```java Display display = getWindowManager().getDefaultDisplay(); Display.Cutout cutout = display.getCutout(); Rect rect = cutout.getBoundingRects().get(0); // 获取第一个切口矩形 ``` - **设置安全边距** 对于可能被遮挡的内容部分增加适当的安全间距: ```java int topInset = getStatusBarHeight(); // 自定义函数用于测量高度 FrameLayout contentFrame = findViewById(R.id.content_frame); contentFrame.setPadding(0, topInset, 0, 0); ``` #### 4. 跨版本兼容性考量 不同的Android版本提供了各自独特的API支持沉浸式功能,因此必须做好充分测试以保障一致性[^3]: | API Level | 方法描述 | |----------------|------------------------------------------------------------------------| | >= Lollipop (21)| `setStatusBarColor()` 和 `FLAG_TRANSLUCENT_STATUS` | | >= KitKat (19) | 修改窗口标志位配合半透明效果 (`FLAG_TRANSLUCENT_STATUS`) | 对于低于Lollipop的设备,可以通过第三方库(如[ImmersionBar](https://github.com/gyf-dev/ImmersionBar))简化复杂度较高的跨平台逻辑。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值