Android狀態欄與頁面頂部內容重合解決方案

在項目的開發過程當中 , 發現建立activity界面後 , 界面頂部的返回按鈕被狀態欄遮擋住一部分 , 在點擊返回的時候,很難觸發點擊事件,頁面也不太美觀,話很少說,直接上代碼.android

計算狀態欄高度的工具類:bash

public class StatusBarUtil {

    //獲取狀態欄高度
    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;
    }

}
複製代碼

image.gif

在activity中重寫onWindowFocusChanged()方法:ide

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //設置第一個view距離狀態欄的高度;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rlLinearLayout.getLayoutParams();//rlLinearLayout爲遮擋住的頁面佈局LinearLayout
        int top = StatusBarUtil.getStatusBarHeight(this);//獲取狀態欄高度
        lp.topMargin = top;
         rlLinearLayout.setLayoutParams(lp);
 }
複製代碼

image.gif

總結: 解決該問題的方案主要是:計算出狀態欄的高度 , 而後用代碼的形式將頁面頂部佈局向下移狀態欄的高度 , 解決方案仍是比較簡單的.
複製代碼

如下是我的公衆號,以後發佈的文章會同步到該公衆號,方便交流學習Android知識及分享我的愛好文章:工具

image

image.gif
相關文章
相關標籤/搜索