How to add a dynamically created LinearLayout to a

2019-05-26 08:01发布

I want to dynamically add some widgets like so:

LinearLayout llay = new LinearLayout(this); 
llay.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
llp.weight = 1.0f; 

CheckBox cb = new CheckBox(getApplicationContext()); 
cb.setText("1"); 
cb.setLayoutParams(llp); 
llay.addView(cb);

ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost);
svh.AddView(llay);

...but I'm getting, "The method AddView(LinearLayout) is undefined for the type ScrollView"

So what should I do to add the LinearLayout to the existing ScrollView?

3条回答
相关推荐>>
2楼-- · 2019-05-26 08:12

Because Java is case sensitive? AddView() != addView(). Also (though not the root of the problem), note that a ScrollView can only have one child.

查看更多
ら.Afraid
3楼-- · 2019-05-26 08:26

Is your problem solved by the dmon's answer?

Be sure that yout have correct the spealing of the addView(). java is case sensitive. addView() and AddView() both are different thing.

Also make sure that your are adding ScrollView to the OneParent Layout. As Because you can able to add the ScrollView to the Only One Parent Layout.

So, supose you have two layout like LinearLayout and RelativeLayout and if you provide One ScrollView to the both layout then it will arrise error.

you have to add another layout as parent of both the layout and then add ScrollView to that parent layout.

Hope you got the point.

Thanks.

Enjoy. :)

查看更多
别忘想泡老子
4楼-- · 2019-05-26 08:31

It's addView(), not AddView(). Or was that just a typo in your question?

查看更多
登录 后发表回答