Android获取屏幕宽高、屏幕密度、手动计算view大小

1、ldpi、mdpi、hdpi、xhdpi、xxhdpi
Android获取屏幕宽高、屏幕密度、手动计算view大小_第1张图片

2、计算屏幕密度
Android获取屏幕宽高、屏幕密度、手动计算view大小_第2张图片

3、Android APP图标大小
Android获取屏幕宽高、屏幕密度、手动计算view大小_第3张图片

4、获取状态栏高度

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

5、获取屏幕长宽常用方法
方法一:

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();

方法二:

WindowManager wm1 = this.getWindowManager();
int width = wm1.getDefaultDisplay().getWidth();
int height = wm1.getDefaultDisplay().getHeight();

方法三:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int mScreenWidth = dm.widthPixels;
int mScreenHeight = dm.heightPixels;

6、手动计算View高度,动态布局

private void adjustViewPagerLayoutParams() {
        final int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        final int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        ViewTreeObserver vto = chartPager.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                chartPager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                WindowManager wm = (WindowManager) ProjectActivity.this.getSystemService(Context.WINDOW_SERVICE);
                int height = wm.getDefaultDisplay().getHeight();
                footerView.measure(w, h);
                LinearLayout lltitle = (LinearLayout) headerView.findViewById(R.id.chart_title);
                lltitle.measure(w, h);
                int viewHeight = lltitle.getMeasuredHeight() + footerView.getMeasuredHeight() + (int) (getResources().getDimension(R.dimen.normal_title_bar_h) * 2.5);
                chartPager.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, height - viewHeight)); //这里的高绝对不能是WRAP_CONTENT  
            }
        });
    }

你可能感兴趣的:(Android,日常积累)