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

1937

被折叠的 条评论
为什么被折叠?



