我在XML文件中用了一个写了一个ScrollView控件,但在XML文件中ScrollView里没有东西现在我想使用代码从远程获取数据,以Button的形式展示出来,然后添加到ScrollView里面来显示,怎么做??已写的有:
ScrollView scroll = (ScrollView) findViewById(R.id.tjzx_scroll);TableLayout table = new TableLayout(this);for (int i = 0; i < res.length; i++) {
TableRow row = new TableRow(this);
Button button1 = new Button(this);
button1.setBackgroundResource(R.drawable.back);
button1.setText(res[i]);
button1.setTextSize(20);
row.addView(button1);
table.addView(row);
}scroll.addView(table);setContentView(R.layout.main);在执行scroll.addView(table);报错,我应该怎么解决??或者说使用哪种方法来解决像类似这种情况的问题??
ScrollView scroll = (ScrollView) findViewById(R.id.tjzx_scroll);TableLayout table = new TableLayout(this);for (int i = 0; i < res.length; i++) {
TableRow row = new TableRow(this);
Button button1 = new Button(this);
button1.setBackgroundResource(R.drawable.back);
button1.setText(res[i]);
button1.setTextSize(20);
row.addView(button1);
table.addView(row);
}scroll.addView(table);setContentView(R.layout.main);在执行scroll.addView(table);报错,我应该怎么解决??或者说使用哪种方法来解决像类似这种情况的问题??
解决方案 »
- 百度地图api实现service后台定位
- 新手请教下不同分辨率的问题
- 运行时控制台总是出现emulator-5554 disconnected,模拟器不能运行
- android本地搜索,quicksearchbox源码研究问题
- 【求救】eclipse环境配置问题
- android 后台自动发送mail
- android控件WebView处理frameset时背景不能透明
- [100分奉上]Android编程遇到问题,全部按照视频里编的。剪切板传输数据。附全部代码。
- 有没有大神用IntelliJ IDEA开发Android的,请求问题,关于设置数据库(databases)的
- 关于LruCache的小疑惑
- 如何在android源码中添加自己的jni方法
- 想业余做ANDROID的开发,有经验的人来指点指点,谢谢。
然后向此Inflate添加你的控件
然后将此Inflate 设置给Activity
下面的答案可以参考下。
首先要却这个界面的布局,是AbsoluteLayout,RelativeLayout还是其他,然后就可以再里面添加控件了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//确定界面的布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
//创建一个button按钮
Button btn1 = new Button(this);
btn1.setText(”this is a button”);
btn1.setId(1);
//确定这个控件的大小和位置
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1 );}
一个界面可以布置一个布局,可以多个布局一起设计:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//设置界面的布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);//添加一个AbsoluteLayout子布局,并给这个布局添加一个button
AbsoluteLayout abslayout=new AbsoluteLayout (this);
abslayout.setId(11);
Button btn1 = new Button(this);
btn1.setText(”this is a abslayout button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1, lp0 );
//将这个子布局添加到主布局中
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);//再添加一个子布局
RelativeLayout relativeLayout1 = new RelativeLayout(this);
Button btn2 = new Button(this);
btn2.setText(”this is a relativeLayout1 button”);
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout1.addView(btn2 ,lp2);//将这个布局添加到主布局中
RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW ,11);
relativeLayout.addView(relativeLayout1 ,lp11);
}