How to add a button dynamically in Android?

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

How to add a button dynamically in Android?

17条回答
  •  盖世英雄少女心
    2020-11-22 13:39

    Try this:

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
    
    Button btn = new Button(this);
    btn.setText("Manual Add");
    btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    ll.addView(btn);
    

提交回复
热议问题