2017-09-30 47 views
1

我正在嘗試創建一個非常基本的計算器佈局(沒有邏輯)。我遇到了一個非常奇怪的問題,那就是按鈕繼續超出屏幕的極限。 「AC」按鈕右側還有一些我不能解釋的神祕空間。下圖是我部署APK後看到的內容。問題當編程添加GridLayout溢出屏幕

enter image description here

這裏

例子是我的代碼:

public void initializeGUI() 
{ 
    Point size = new Point(); 
    getWindowManager().getDefaultDisplay().getSize(size); 

    int width = (size.x/COLUMN_SIZE); 
    int height = (size.y/ROW_SIZE); 

    GridLayout gridLayout = new GridLayout(this); 
    ScrollView scrollView = new ScrollView(this); 
    Button [] [] buttons = new Button[ROW_SIZE - 3] [COLUMN_SIZE]; 
    String [] [] buttonsTxt = 
      { 
       {"7","8","9","X"}, 
       {"4","5","6","+"}, 
       {"1","2","3","-"} 
      }; 
    TextView result = setupView(new TextView(this), (width * COLUMN_SIZE), height, "#FFE4D4", Gravity.RIGHT, "0", createParams(0, 1, 0, COLUMN_SIZE)); 
    Button reset = setupView(new Button(this), width * 2, height, "#808000", Gravity.CENTER, "AC", createParams(1, 1, 0, 2)); 
    Button percent = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "%", createParams(1, 1, 2, 1)); 
    Button divide = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "/", createParams(1, 1, 3, 1)); 
    Button zero = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, "0", createParams(5, 1, 0, 1)); 
    Button equals = setupView(new Button(this), (width * 3), height, "#808000", Gravity.CENTER, "=", createParams(5, 1, 1, 3)); 

    gridLayout.setColumnCount(COLUMN_SIZE); 
    gridLayout.setRowCount(ROW_SIZE); 
    gridLayout.setBackgroundColor(Color.BLACK); 

    gridLayout.addView(result); 
    gridLayout.addView(reset); 
    gridLayout.addView(percent); 
    gridLayout.addView(divide); 

    for(int row=0; row<buttons.length; row++) 
    { 
     for(int col=0; col<buttons[row].length; col++) 
     { 
      buttons [row] [col] = setupView(new Button(this), width, height, "#C0C0C0", Gravity.CENTER, buttonsTxt[row] [col], createParams(row + 2, 1, col, 1)); 
      gridLayout.addView(buttons [row] [col]); 
     } 
    } 

    gridLayout.addView(zero); 
    gridLayout.addView(equals); 
    scrollView.addView(gridLayout); 
    setContentView(scrollView); 
} 

public GridLayout.LayoutParams createParams(int startingRow, int rowSize, int startingCol, int colSize) 
{ 
    GridLayout.Spec rowSpec = GridLayout.spec(startingRow, rowSize); 
    GridLayout.Spec colSpec = GridLayout.spec(startingCol, colSize); 

    GridLayout.LayoutParams glp = new GridLayout.LayoutParams(rowSpec, colSpec); 

    glp.topMargin = 7; 
    glp.rightMargin = 7; 

    return glp; 
} 


public Button setupView(Button b, int width, int height, String color, int gravity, String text, GridLayout.LayoutParams glp) 
{ 
    b.setWidth(width); 
    b.setHeight(height); 
    b.setBackgroundColor(Color.parseColor(color)); 
    b.setGravity(gravity); 
    b.setText(text); 
    b.setTextSize(fontSize); 
    b.setLayoutParams(glp); 

    return b; 
} 

public TextView setupView(TextView tv, double width, double height, String color, int gravity, String text, GridLayout.LayoutParams glp) 
{ 
    tv.setWidth((int)width); 
    tv.setHeight((int)height); 
    tv.setBackgroundColor(Color.parseColor(color)); 
    tv.setGravity(gravity); 
    tv.setText(text); 
    tv.setTextSize(fontSize); 
    tv.setLayoutParams(glp); 

    return tv; 
} 
} 

回答

0

原因你的麻煩是因爲setWidth()setHeight()沒有做什麼,你需要他們做的(他們基本上什麼都不做)。我不知道爲什麼會如此(同樣的問題也讓我感到困擾,而我卻不知道)。

可以,而不是使用setWidth()setHeight(),使用此:

GridLayout.LayoutParams params=(GridLayout.LayoutParams)tv.getLayoutParams(); 
params.width=(int)width; 
params.height=(int)height; 
tv.setLayoutParams(params); 

然而,在這種情況下,使用LayoutParams只能如果視圖已經添加,即你有後,你應該只使用代碼執行setContentView(scrollView)。您需要創建另一個小功能,您需要傳遞視圖以及它們各自的寬度和高度。

這也將解決您在「AC」按鈕右側多出一點空間的問題。這是由於GridLayout爲列提供了統一的寬度(等於該列中最寬的視圖),而不用擔心所有視圖是否佔用全部寬度,並且setWidth()未將所需寬度應用於「AC」按鈕。

+0

這解決了大部分問題。不過,設置邊距時似乎存在大部分問題。 GridLayout沒有考慮我添加的7px頂部和右邊距。這導致一些行比其他行更寬(從而超出屏幕邊界)。經過寬度變量的一些調整後,我能夠得到可用的東西。感謝您的幫助! – jmknight2