-更新-
我按照 Agarwal 的建议做了,现在有这个错误:
04-21 11:42:01.807: E/AndroidRuntime(1456): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
我正在使用此代码动态设置 15 个按钮的宽度。而且,你猜对了,它不起作用。错误发生在for循环中,但我不知道为什么。
    Button[] buttons = new Button[16];
    buttons[0] = (Button)findViewById(R.id.root2);
    buttons[1] = (Button)findViewById(R.id.root3);
    /* blah blah blah */
    buttons[13] = (Button)findViewById(R.id.root15);
    buttons[14] = (Button)findViewById(R.id.root16);
    LayoutParams lp = new LayoutParams(widthOfButtons,LayoutParams.WRAP_CONTENT);
    for(int x = 0; x < 16; x ++){
        buttons[x].setLayoutParams(lp);
    }
谢谢你的帮助。(如果有人能想到一个更好的方法来填充buttons[]变量,那将非常感激。)