1
我正在嘗試創建一個非常基本的計算器佈局(沒有邏輯)。我遇到了一個非常奇怪的問題,那就是按鈕繼續超出屏幕的極限。 「AC」按鈕右側還有一些我不能解釋的神祕空間。下圖是我部署APK後看到的內容。問題當編程添加GridLayout溢出屏幕
這裏
例子是我的代碼:
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;
}
}
這解決了大部分問題。不過,設置邊距時似乎存在大部分問題。 GridLayout沒有考慮我添加的7px頂部和右邊距。這導致一些行比其他行更寬(從而超出屏幕邊界)。經過寬度變量的一些調整後,我能夠得到可用的東西。感謝您的幫助! – jmknight2