Button
Java コードを使用してビューに を追加したいのですがmain_activity
、どうすればよいですか? 私はすでにこのコードを試しましたが、残念ながらうまくいきませんでした
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout l1 = ((RelativeLayout)this.findViewById(R.id.view1));
btn = new Button(this);
btn.setText(R.string.hello_world);
l1.addView(btn);
setContentView(l1);
}
}