Android:Java代码添加按钮

时间:2021-11-08 05:45:58
        LinearLayout layout = (LinearLayout) findViewById(R.id.container);

        Button bn = new Button(this);
bn.setText("点击");
bn.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)); layout.addView(bn); bn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
ComponentName comp = new ComponentName(MainActivity.this, FullscreenActivity02.class);
Intent intent = new Intent();
intent.setComponent(comp);
startActivity(intent);
}
});

寻:通过id寻找布局Layout
新:新建按钮
设:设置按钮的文字,式样(new Layout)
加:讲按钮加入布局