How to add a button dynamically in Android?

后端 未结 17 1142
礼貌的吻别
礼貌的吻别 2020-11-22 13:17

How to add a button dynamically in Android?

17条回答
  •  醉酒成梦
    2020-11-22 13:33

    public void add_btn() {
    
        lin_btn.setWeightSum(3f);
        for (int j = 0; j < 3; j++) {
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params1.setMargins(10, 0, 0, 10);
            params1.weight = 1.0f;
    
            LinearLayout ll;
            ll = new LinearLayout(this);
            ll.setGravity(Gravity.CENTER_VERTICAL);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setLayoutParams(params1);
    
            final Button btn;
            btn = new Button(DynamicActivity.this);
    
            btn.setText("A"+(j+1));
            btn.setTextSize(15);
            btn.setId(j);
            btn.setPadding(10, 8, 10, 10);
    
            ll.addView(btn);
    
            lin_btn.addView(ll);
    
    
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if(v.getId()==0)
                    {
                        txt_text.setText("Hii");
                    }else if(v.getId()==1)
                    {
                        txt_text.setText("hello");
                    }else if(v.getId()==2)
                    {
                        txt_text.setText("how r u");
                    }
    
    
    
                }
            });
        }
    
    }
    

提交回复
热议问题