版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
1.擷取狀态欄高度:
decorView是window中的最頂層view,可以從window中擷取到decorView,然後decorView有個getWindowVisibleDisplayFrame方法可以擷取到程式顯示的區域,包括标題欄,但不包括狀态欄。
于是,我們就可以算出狀态欄的高度了。
Rect frame = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;
2.擷取标題欄高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個方法擷取到的view就是程式不包括标題欄的部分,然後就可以知道标題欄的高度了。
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//statusBarHeight是上面所求的狀态欄的高度int titleBarHeight = contentTop - statusBarHeight
例子代碼:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
本文轉自 一點點征服 部落格園部落格,原文連結:http://www.cnblogs.com/ldq2016/p/6835368.html,如需轉載請自行聯系原作者