1. 程式人生 > >android 獲取手機解析度、狀態列、…

android 獲取手機解析度、狀態列、…

480-800,480X854的狀態列是38
720-1280 的是56

如何通過程式碼獲取手機螢幕的解析度,程式碼其實很簡單,關鍵是android.util.DisplayMetrics這個類。該類記錄了一些常用資訊,如顯示資訊、大小、維度、字型等等。

publicvoid onCreate(Bundle savedInstanceState) {  

super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

DisplayMetrics dm = new DisplayMetrics();  

        getWindowManager().getDefaultDisplay().getMetrics(dm);  

int width = dm.widthPixels;  

int height = dm.heightPixels;  

       Button button = (Button)findViewById(R.id.b01);  

      button.setText("手機螢幕解析度為:"+width+"*"+height);  

}  

Android下獲取狀態列的高度


    public static int getStatusHeight(Activity activity){
        int statusHeight = 0;
        Rect localRect = new Rect();
        activity.getWindow().getDecorView(
        ).getWindowVisibleDisplayFrame(localRect);
        statusHeight = localRect.top;
        if (0 == statusHeight){
            Class<?> localClass;
            try {
                localClass = Class.forName(
            "com.android.internal.R$dimen");
                Object localObject = localClass.newInstance();
                int i5 = Integer.parseInt(
              localClass.getField("status_bar_height").get(
                localObject).toString());
                statusHeight = activity.getResources(
            ).getDimensionPixelSize(i5);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {