编程记录1

为FrameLayout设置大小的代码如下:

1
2
3
4
5
6
7
8
9
mAdvertFrame = (FrameLayout) findViewById(R.id.photos_change);
LayoutInflater mInflater = getLayoutInflater();
mAdvertFrame.setPadding(5, 5, 5, 0);
// 五分之二高度
int wheight = getWindowManager().getDefaultDisplay().getHeight();
wheight = (int) (wheight * 0.25);
FrameLayout.LayoutParams frame_params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, wheight);
mAdvertFrame.setLayoutParams(frame_params);

结果出现一下错误: 设置LayoutParams错误java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widg查得原因为: ### LinearLayout为子控件分配空间的时候,获取FrameLayout的LayoutParams的必须为LinearLayout.LayoutParams,而非FrameLayout.LayoutParams。 修改代码如图: 修改后代码
解决。