How to add a button dynamically in Android?

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

How to add a button dynamically in Android?

相关标签:
17条回答
  • 2020-11-22 13:39
    Button myButton = new Button(this);
    myButton.setId(123);
    myButton.setText("Push Me");
    
    
    LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
     myButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast.makeText(DynamicLayout.this,
                            "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                            .show();
                }
            });
    
    0 讨论(0)
  • 2020-11-22 13:40

    try this

    private void createLayoutDynamically(int n) {
    
        for (int i = 0; i < n; i++) {
            Button myButton = new Button(this);
            myButton.setText("Button :"+i);
            myButton.setId(i);
            final int id_ = myButton.getId();
    
            LinearLayout layout = (LinearLayout) findViewById(R.id.myDynamicLayout);
            layout.addView(myButton);
    
            myButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Toast.makeText(DynamicLayout.this,
                            "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                            .show();
                }
            });
        }
    
    0 讨论(0)
  • 2020-11-22 13:42
    Button myButton = new Button(this);
    myButton.setText("Push Me");
    
    LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
    

    Have a look to this example

    0 讨论(0)
  • 2020-11-22 13:49

    Try this code

     Button btn=new Button(this);
    btn.setId(btn);
    btn.setBackgroundResource(R.drawable.image);
    btn.setMinimumHeight(150);
    btn.setMinimumWidth(150);
    Relativelayout.addView(btn); 
    
    0 讨论(0)
  • 2020-11-22 13:49

    Try this code. It will work fine..

    public class DynamicViewsActivity extends Activity {
    
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_dynamic_views);
        ScrollView scrl=new ScrollView(this);
        final LinearLayout ll=new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(100, 500, 100, 200);
        scrl.addView(ll);
        Button add_btn=new Button(this);
        add_btn.setText("Click Here");
    
        ll.addView(add_btn, layoutParams);
    
    
        final Context context = this;
    
        add_btn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                Intent intent = new Intent(context, App2Activity.class);
                startActivity(intent);
            }
        });
        this.setContentView(scrl);
    }
    }
    
    0 讨论(0)
  • 2020-11-22 13:50
    Button btn = new Button(this);
    btn.setText("Submit");
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.buttonlayout);
    LayoutParams buttonlayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    linearLayout.addView(btn, buttonlayout);
    
    0 讨论(0)
提交回复
热议问题