我想让用户添加按钮,这样每一行只有四个按钮。所以我写了以下函数:
private void addContact() {
//numButton Count how many buttons there are in line
if(numButton==0){
LinearLayout linearLayout =new LinearLayout(this);
linearLayout.setOrientation(0);//horizontal
ImageButton imageButton =new ImageButton(this);
imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
linearLayout.addView(imageButton);
LinearLayout linearbase= (LinearLayout)findViewById(R.id.linearBase);
linearbase.addView(linearLayout);
numButton++;
}
else if(numButton<4)
{
LinearLayout linearlayout= ----####Here I do not know what to write!!!!###
ImageButton imageButton =new ImageButton(this);
imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
linearlayout.addView(imageButton);
numButton++;
}
else
{
numButton=0;
}
}
我标记了我的问题的代码行具体来说,我的问题是如何将新按钮放入之前调用此函数中定义的线性布局中?第二个问题:即使关闭应用程序,如何保持新的情况?