Android 如何动态添加按钮

Android 如何动态添加按钮,android,android-widget,Android,Android Widget,我想让用户添加按钮 所以每行只有四个按钮。 因此,我编写了以下函数: private void addContact() { //numButton Count how many buttons there are in line if(numButton==0){ LinearLayout linearLayout =new LinearLayout(this); linearLayout.setOrientati

我想让用户添加按钮 所以每行只有四个按钮。 因此,我编写了以下函数:

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;
        }
    }
private void addContact(){
//numButton数一数一行有多少个按钮
if(numButton==0){
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(0);//水平
ImageButton ImageButton=新建ImageButton(此);
setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.button1));
linearLayout.addView(图像按钮);
LinearLayout linearbase=(LinearLayout)findViewById(R.id.linearbase);
linearbase.addView(linearLayout);
numButton++;
}

否则,如果第一次创建LinearLayout时(numButton给它一个id。然后你可以使用findViewById()第二次获取它。要回答第二个问题,请在中存储一个布尔值。

在addContact方法外声明你的LinearLayout,否则它们只存在于该方法内。我认为这样应该可以(我还没有测试过):

class-myclass{
私人线性布局线性布局;
私人线路布局线路基础;
私人内特纽顿;
@凌驾
公共空间膨胀(){
超级充气();
linearbase=(LinearLayout)findviewbyd(R.id.linearbase);
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(0);//水平
numButton=0;
}
私有void addContact(){
//numButton数一数一行有多少个按钮
if(numButton==0){
ImageButton ImageButton=新建ImageButton(此);
setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.button1));
linearLayout.addView(图像按钮);
linearbase.addView(linearLayout);
numButton++;
}
else if(numbutton)这可能会有所帮助:我在相关列(那边-->)中找到了它。至于在关闭应用程序后保留数据,这是一个很好的参考。
class myclass{

    private LinearLayout linearLayout;
    private LinearLayout linearbase;
    private int numButton;

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();

        linearbase= (LinearLayout)findViewById(R.id.linearBase);
        LinearLayout linearLayout =new LinearLayout(this);
        linearLayout.setOrientation(0);//horizontal

        numButton=0;
    }

    private void addContact() {
        //numButton Count how many buttons there are in line
        if(numButton==0){

            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearLayout.addView(imageButton);
            linearbase.addView(linearLayout);
            numButton++;
        }
        else if(numButton<4)
        {
            linearLayout= new LinearLayout(this);
            ImageButton imageButton =new ImageButton(this);
            imageButton.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button1));
            linearlayout.addView(imageButton);
            numButton++;
        }
        else 
        {
            numButton=0;
        }
    }   
}