android动态添加控件,调试报错
以下是代码,目的是在现有的一个scrollview里添加一个tablelayout,然后逐行添加按键新手入门,不太了解。望指教publicvoidAddBtn(){scr...
以下是代码,目的是在现有的一个scrollview里添加一个tablelayout,然后逐行添加按键
新手入门,不太了解。望指教
public void AddBtn(){
scrollV = (ScrollView)findViewById(R.id.scrollView1);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setOrientation( TableLayout.VERTICAL );
tableLayoutParams = new TableLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
scrollV.addView(tableLayout);
setContentView(tableLayout);
TableRow tablerow1 =new TableRow(this);
tableLayout.addView(tablerow1);
Button btn1 = new Button(this);
btn1.setHeight(70);
btn1.setWidth(214);
tablerow1.addView(btn1);
}
还有一个问题,button添加好以后要如何使用该按钮。。。动态添加的按钮不是没ID的么。。。。 展开
新手入门,不太了解。望指教
public void AddBtn(){
scrollV = (ScrollView)findViewById(R.id.scrollView1);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setOrientation( TableLayout.VERTICAL );
tableLayoutParams = new TableLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
scrollV.addView(tableLayout);
setContentView(tableLayout);
TableRow tablerow1 =new TableRow(this);
tableLayout.addView(tablerow1);
Button btn1 = new Button(this);
btn1.setHeight(70);
btn1.setWidth(214);
tablerow1.addView(btn1);
}
还有一个问题,button添加好以后要如何使用该按钮。。。动态添加的按钮不是没ID的么。。。。 展开
1个回答
展开全部
Button test = null;
ScrollView scrollV = null;
TableLayout.LayoutParams tableLayoutParams;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test = (Button)findViewById(R.id.click);
test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AddBtn();
}
});
scrollV = (ScrollView)findViewById(R.id.scrollView1);
tableLayout = new TableLayout(this);
tableLayout.setOrientation( TableLayout.VERTICAL );
tableLayoutParams = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
scrollV.addView(tableLayout);
}
TableLayout tableLayout;
public void AddBtn(){
//setContentView(tableLayout);
TableRow tablerow1 =new TableRow(this);
tableLayout.addView(tablerow1);
Button btn1 = new Button(this);
btn1.setHeight(70);
btn1.setWidth(214);
tablerow1.addView(btn1);
}
ScrollView scrollV = null;
TableLayout.LayoutParams tableLayoutParams;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test = (Button)findViewById(R.id.click);
test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AddBtn();
}
});
scrollV = (ScrollView)findViewById(R.id.scrollView1);
tableLayout = new TableLayout(this);
tableLayout.setOrientation( TableLayout.VERTICAL );
tableLayoutParams = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
scrollV.addView(tableLayout);
}
TableLayout tableLayout;
public void AddBtn(){
//setContentView(tableLayout);
TableRow tablerow1 =new TableRow(this);
tableLayout.addView(tablerow1);
Button btn1 = new Button(this);
btn1.setHeight(70);
btn1.setWidth(214);
tablerow1.addView(btn1);
}
追问
还有个问题,button添加好以后要如何使用该按钮。。。动态添加的按钮不是没ID的么。。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询