我正在尝试在线性布局上以编程方式创建按钮,我想要按钮的“列”,但似乎线性布局自动将按钮放在彼此之下,我找不到一种方法来使我的按钮位置再次登顶
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_branch);
LinearLayout linear = (LinearLayout) findViewById(R.id.content);
for(int i =0; i <10; i++) {
Button btn = new Button(this);
btn.setText("Branch1");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(330,200);
layoutParams.setMargins(rowNb*330+5, 3, 0, 0); // left, top, right, bottom
btn.setLayoutParams(layoutParams);
linear.addView(btn);
btnNb++;
if(btnNb>5)
{
rowNb = 1;
}
}
}
有人可以帮助我找到像第一列一样制作第二列的方法吗?我已经尝试了btn.SetY或btn.setGravity
答案 0 :(得分:0)
尝试使用GridLayout而不是LinearLayout https://developer.android.com/reference/android/widget/GridLayout.html