我想在运行时设置按钮宽度,我希望它等于显示宽度的50%。
我使用了以下内容,但遇到“模拟器中什么都没有出现”的问题,并说
“进程意外停止。请重试”
.java
    公共课程实验扩展了活动{

    @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(width);
没有错误,并且布局出现了,那么在.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/

10-11 22:21
查看更多