我的按钮大小不会改变

时间:2014-03-16 04:30:38

标签: java android android-layout

以下是我更改按钮按钮大小的代码:

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;

    int Orientation = getResources().getConfiguration().orientation;

    if(Orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setLayoutParams(new LayoutParams(10,100));
        Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setLayoutParams(new LayoutParams(50,50));
        Button btn3 = (Button) findViewById(R.id.button3);
        btn3.setLayoutParams(new LayoutParams(height/9,width/6));
        Button btn4 = (Button) findViewById(R.id.button4);
        btn4.setLayoutParams(new LayoutParams(height/9,width/6));
        Button btn5 = (Button) findViewById(R.id.button5);
        btn5.setLayoutParams(new LayoutParams(height/9,width/6));
        Button btn6 = (Button) findViewById(R.id.button6);
        btn6.setLayoutParams(new LayoutParams(height/9,width/6));

现在无论我在所有按钮的宽度和高度部分大小中放置什么值,都保持不变。如何更改它们的大小。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法,

LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 50;
params.height = 50;
button.setLayoutParams(params);

button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));