设置控件的大小,DP

2019-04-22  本文已影响0人  谜之龙

//普通的设置

public static void setViewSize(View view, int width, int height){
    Log.e("设置上下","宽="+width+"高="+height);
    ViewGroup.LayoutParams params = view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

//根据DP来设置,其中displayMetrics必须在Activity中设置

DisplayMetrics  displayMetrics=getResources().getDisplayMetrics();
public static void setViewSizeDp(View view, int width, int height){
    Log.e("设置上下","宽="+width+"高="+height);
    ViewGroup.LayoutParams params = view.getLayoutParams();
      params.width =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width,displayMetrics);
    params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height,displayMetrics);
    view.setLayoutParams(params);
}
上一篇下一篇

猜你喜欢

热点阅读