How to add a dynamically created LinearLayout to a ScrollView?

孤街浪徒 提交于 2019-12-10 11:15:50

问题


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?


回答1:


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




回答2:


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




回答3:


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. :)



来源:https://stackoverflow.com/questions/9509842/how-to-add-a-dynamically-created-linearlayout-to-a-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!