Android 获取控件的高度或者宽度的方法

方法一:

DisplayMetrics dm = getResources().getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;

方法二:

/**
 * 获取控件的高度或者宽度  isHeight=true则为测量该控件的高度,isHeight=false则为测量该控件的宽度
 * @param view
 * @param isHeight
 * @return
 */
public static int getViewHeight(View view, boolean isHeight){
    int result;
    if(view==null)return 0;
    if(isHeight){
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(h,0);
        result =view.getMeasuredHeight();
    }else{
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(0,w);
        result =view.getMeasuredWidth();
    }
    return result;
}

你可能感兴趣的:(Android,tips)