我想在运行时设置按钮宽度,我希望它等于显示宽度的 50%。 我使用了以下但我有一个问题“模拟器中什么都没有出现”并说 “进程意外停止。请重试” .java 公开课实验扩展 Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Button btn = (Button) findViewById(R.id.btnmarwa);
btn.setWidth(width/2);
setContentView(R.layout.emailpopup);
}
}
当我评论这一行时
btn.setWidth(宽度);
没有报错,出现了布局,那么在.java文件中在哪里给按钮设置新的宽度呢??
最佳答案
不是直接关于您的问题,但您必须将此行放在 setContentView() 之后,因为 findViewById 方法。
像这样试试:
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.emailpopup);
Button btn = (Button) findViewById(R.id.btnmarwa);
btn.setWidth(width/2);
关于android - 如何在运行时设置按钮的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574465/