Lpc*_*ark -2 xml android dynamic textview android-scrollview
所以从调试和评论出来的事情我得出结论,这个程序的问题是在addTextView()方法中.我完全不知道问题出在哪里.我的猜测是在我尝试添加到TextView的布局中.非常感谢你提前帮助.
public class Books extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.books);
addTextView("Hello World");
}
public void addTextView(String text)
{
ScrollView viewport = (ScrollView)findViewById(R.id.books);
TextView textview= (TextView)new TextView(this);
textview.setText(text);
//textview.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
viewport.addView(textview, new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public void addButton(String text)
{
ScrollView scrollview = (ScrollView)findViewById(R.id.books);
Button btnTag = (Button)new Button(Books.this);
btnTag.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText(text);
scrollview.addView(btnTag);
}
}
Run Code Online (Sandbox Code Playgroud)
ScrollViews只能有一个子节点,通常是LinearLayout.当你打电话给scrollview.addView(blah blah)
你时,你会向ScrollView添加更多的孩子,这是不允许的.
有关ScrollView的文档,请参见此处.
-
另外,在StackOverflow上发布有关您遇到的错误的问题时,请说明:
它会帮助每个人,包括未来偶然发现这个问题的人.
归档时间: |
|
查看次数: |
572 次 |
最近记录: |