我给你留下了代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.disposizione);
Button pulsante = new Button(this);
pulsante.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
pulsante.setText("Click me");
pulsante.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Thug life bitch!", Toast.LENGTH_LONG).show();
}
});
layout.addView(pulsante);
setContentView(R.layout.main);
}
我尝试将所有行从RelativeLayout删除到layout.addView,然后再次运行!
此代码有什么问题?我必须说我没有收到任何构建错误或警告。
提前致谢。
答案 0 :(得分:3)
在setContentView()
super.onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.main);