-1
我想使用java代碼將Button
添加到main_activity
視圖,那麼我該怎麼做呢? 我已經嘗試過此代碼,不幸的是它沒有工作使用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);
}
}
在設置contentView之前,您無法調用'findViewById'。 – Ahmad