现在我定义了两个类并都继承了view,但是在Activity中用addView分别添加这两个时,结果却只能显示出最前面一个addView添加的view,第二个不能显示,为什么阿?
LinearLayout layout = new LinearLayout(this);
ball = new MyBallview(this);
board = new MyBoardview(this);
layout.addView(ball);
layout.addView(board);
setContentView(layout, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
你的内容样式是FILL_PARENT,当然只能显示出一个了,FILL_PARENT就是填充它的PARENT。
1 LinearLayout layout = new LinearLayout(this);
2 layout.setOrientation(LinearLayout.VERTICAL);
3
4 ball = new MyBallview(this);
5 layout.addView(ball);
6
7 board = new MyBoardview(this);
8 System.out.println("222");
9 layout.addView(board);
10
11
12 System.out.println("333");
13 setContentView(layout, new LinearLayout.LayoutParams(
14 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
还是只能显示出第一个,也就是ball这个view
用FrameLayout试一试吧