我得到了一個GridLayout
,裏面有兩個或更多的按鈕,所有的按鈕都必須具有相同的寬度。我的問題是,當按鈕具有不同的文本行號時,按鈕的marginTop
也會不同。我怎樣才能解決這個問題?我想要所有的按鈕在相同的高度位置。如何以編程方式設置按鈕layout_gravity =「fill_vertical」?
謝謝。
我已經嘗試設置Gravity
與FILL_VERTICAL
,但無法正常工作。
GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors);
grid_neighbor.setColumnCount(4);
for (i = 0; i < namelist.length; ++i) {
Button temp = new Button(this);
temp.setText(namelist[i]);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.FILL_VERTICAL;
temp.setLayoutParams(lp);
//temp.setGravity(Gravity.FILL_VERTICAL);
temp.setWidth(button_size);
temp.setHeight(button_size);
grid_neighbor.addView(temp);
}
這裏是圖片:
現在這是工作壓力太大。
GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors);
grid_neighbor.setColumnCount(4);
for (i = 0; i < namelist.length; ++i) {
Button temp = new Button(this);
temp.setText(namelist[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, temp.getId());
temp.setLayoutParams(lp);
//temp.setGravity(Gravity.FILL_VERTICAL);
temp.setWidth(button_size);
temp.setHeight(button_size);
grid_neighbor.addView(temp);
}
這將工作!謝謝。
GridLayout.Spec speccolumn = GridLayout.spec(i%4);
GridLayout.Spec specrow = GridLayout.spec(i/4, GridLayout.TOP);
grid_neighbor.addView(temp, new GridLayout.LayoutParams(specrow, speccolumn));
添加規則與第一個按鈕對齊頂部 – OMAK
你不能設置按鈕來填充父項的高度嗎? – Hardik4560
@OMAK我嘗試addRule(RelativeLayout.ALIGN_TOP)&ALIGN_PARENT_TOP,兩者都不起作用。 – JamesChiang