我已经检查过现有线程的解决方案。答案总是:
setLayoutParams(new LayoutParams(100,100)
所以我尝试了,但是当我按下按钮时,我的应用程序崩溃了:
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btn.setLayoutParams(new LayoutParams(100, 100));
}
});
}
}
我做错了什么?谢谢!
答案 0 :(得分:3)
根据你获得的例外(ClassCast);你必须告诉它所在的视图。例如。如果你的Button在RelativeLayout中:
RelativeLayout.LayoutParams lyt = new RelativeLayout.LayoutParams(100, 100);
btn.setLayoutParams(lyt);