android 一些测量view的方法

1.获取TitleBar高

public static int getTitleBarHeight(Activity activity) {

// TODO Auto-generated method stub

    DisplayMetrics dm =new DisplayMetrics();

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

int width = dm.widthPixels;//屏幕宽

    int height = dm.heightPixels;//屏幕高

    Rect frame =new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;//状态栏高

    int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

int titleBarHeight = contentTop - statusBarHeight;//标题栏高

    return statusBarHeight;

}

2.获取屏幕的宽高

public static int[] getScreenWH(Activity activity) {

int[] wh =new int[2];

DisplayMetrics dm =new DisplayMetrics();

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

wh[0] = dm.widthPixels;

wh[1] = dm.heightPixels;

return wh;

}

3.获取屏幕的宽高

public static int[] getViewWH(View view) {

int[] wh =new int[2];

view.getLocationInWindow(wh);

return wh;

}

4.设置View的高度

public static void setViewHeight(int height, View v) {

LayoutParams lp = v.getLayoutParams();

lp.height = height;

v.setLayoutParams(lp);

}

调用举例:ViewUtils.setViewHeight(ViewUtils.getScreenWidth(mContext) /9, holder.lv);

5.设置View的宽度

public static void setViewWidth(int width, View v) {

LayoutParams lp = v.getLayoutParams();

lp.width = width;

v.setLayoutParams(lp);

}


6.获取屏幕的宽高

/**

* get Display

*

* @param context

* @return

*/

private static Display getDisplay(Context context) {

Display display = ((WindowManager) context

.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

return display;

}

/**

* get Screen Width

*

* @param context

* @return

*/

@SuppressWarnings("deprecation")

public static int getScreenWidth(Context context) {

if (null == context) {

return 0;

}

return getDisplay(context).getWidth();

}

/**

* get Screen Heght

*

* @param context

* @return

*/

@SuppressWarnings("deprecation")

public static int getScreenHeight(Context context) {

if (null == context) {

return 0;

}

return getDisplay(context).getHeight();

}

7.设置控件宽高的另一种方法

mIv_BannerView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewUtils.getScreenWidth(mContext) *239 /640));

你可能感兴趣的:(android 一些测量view的方法)