设置View的大小是通过设置LayoutParams
参数。
如果一个view在一个RelativeLayout里面,需要用一个RelativeLayout.LayoutParams对象来设置
在代码里面设置的高度height是px,如果想用dp单位设置,需要获取屏幕的密度,然后转换。
final float scale = getActivity().getResources().getDisplayMetrics().density;int height = (int) (48 * scale + 0.5f); // 48dpfinal ViewGroup adContainer = (ViewGroup)getView().findViewById(R.id.ad_test);RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);adContainer.setLayoutParams(params);