Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 程式設計:取得狀態列(通知欄)高度的方法

編輯:關於 Android 程式設計

本文分享一個在 Android 中取得狀態列(通知欄)高度的實例,供大家參考。

這裡透過反射機制來取得通知欄高度。通知欄高度其實被定義在 dimen 檔案中,因此只要讀取對應的資源值再換算成像素即可。

 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); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; } 

希望本文所述能對大家的 Android 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved