获取status bar的高度

    public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}


还能获取些什么属性的值呢? 请看{sdk.home}\platforms\android-21\data\res\values\dimens.xml

    <!-- Height of the status bar -->
<dimen name="status_bar_height">25dip</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">42dp</dimen>
<!-- Height of notification icons in the status bar -->
<dimen name="status_bar_icon_size">24dip</dimen>
<!-- Size of the giant number (unread count) in the notifications -->
<dimen name="status_bar_content_number_size">48sp</dimen>
<!-- Margin at the edge of the screen to ignore touch events for in the windowshade. -->
<dimen name="status_bar_edge_ignore">5dp</dimen>


Reference: [url]http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个效果,需要在 AndroidManifest.xml 文件中设置 activity 的主题为透明,然后在布局文件中添加一个与 statusBar 高度相同的 View,将其背景设置为透明。具体步骤如下: 1. 在 AndroidManifest.xml 文件中设置 activity 的主题为透明: ```xml <activity android:name=".MainActivity" android:theme="@style/TransparentTheme"> </activity> ``` 2. 在 styles.xml 文件中定义一个透明主题: ```xml <style name="TransparentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowTranslucentStatus">true</item> </style> ``` 3. 在布局文件中添加一个与 statusBar 高度相同的 View: ```xml <View android:id="@+id/status_bar" android:layout_width="match_parent" android:layout_height="@dimen/status_bar_height" android:background="@android:color/transparent" /> ``` 其中,@dimen/status_bar_height 是一个定义了 statusBar 高度的 dimen。 4. 在 MainActivity 中获取 statusBar 的高度,并将布局文件中的 View 的高度设置为 statusBar 的高度: ```java // 获取 statusBar 高度 int statusBarHeight = getResources().getDimensionPixelSize( getResources().getIdentifier("status_bar_height", "dimen", "android")); // 设置 statusBar 高度 View statusBar = findViewById(R.id.status_bar); ViewGroup.LayoutParams params = statusBar.getLayoutParams(); params.height = statusBarHeight; statusBar.setLayoutParams(params); ``` 这样就可以实现全屏显示 statusBar,且 statusBar 完全透明显示在 app 前面的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值