如何以编程方式将button逐个添加到布局中?

如何在几行中逐一创buildbutton列表? 我做的:

LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags); for (int i = 1; i < 10; i++) { Button btnTag = new Button(this); btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnTag.setText("Button " + i); btnTag.setId(i); layout.addView(btnTag); ((Button) findViewById(i)).setOnClickListener(this); } 

只有一行:

我懂了
如何以编程方式进入下一行?

问题是你的button不会自动换行到屏幕的下一个部分。 您必须具体告诉Android如何将您的视图定位。 您可以使用ViewGroups(如LinearLayout或RelativeLayout)来执行此操作。

 LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags); layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical" for (int i = 0; i < 3; i++) { LinearLayout row = new LinearLayout(this); row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); for (int j = 0; j < 4; j++ { Button btnTag = new Button(this); btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnTag.setText("Button " + (j + 1 + (i * 4)); btnTag.setId(j + 1 + (i * 4)); row.addView(btnTag); } layout.addView(row); } 

我假设R.id.linear_layout_tags是这个活动的XML的父LinearLayout。

基本上你在这里做的是你正在创build一个LinearLayout,这将是一个持有你的四个button的行。 然后button被添加并且每个按照它们的ID被递增地分配一个数字。 一旦添加了所有的button,该行将被添加到您的活动的布局。 然后重复。 这只是一些伪代码,但它可能会工作。

哦,下一次肯定要花更多的时间在你的问题上…

https://stackoverflow.com/questions/how-to-ask

这就像1个答案,但不需要做一个XML文件。

 public class mainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical" for (int i = 0; i < 3; i++) { LinearLayout row = new LinearLayout(this); row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); for (int j = 0; j < 4; j++) { Button btnTag = new Button(this); btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnTag.setText("Button " + (j + 1 + (i * 4 ))); btnTag.setId(j + 1 + (i * 4)); row.addView(btnTag); } layout.addView(row); } setContentView(layout); //setContentView(R.layout.main); } } 

这将有所帮助

 private void addingParticipantinFrame() { TableRow row; final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp); add_button_particiapants.setVisibility(View.VISIBLE); if (arrrItemSelcted != null && arrrItemSelcted.size() > 0) { arrrItemSelcted.remove(0); int i = 0; int j = 0; int width = (screenWidth - (arrrItemSelcted.size())*4)/arrrItemSelcted.size(); while (i<arrrItemSelcted.size()) { j = i + 4; row = new TableRow(mParentActivity); TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT); row.setLayoutParams(lp); row.setWeightSum(4); while ((i<j)&&(i<arrrItemSelcted.size())) { Button iBtn = new Button(mParentActivity); iBtn.setGravity(Gravity.CENTER_HORIZONTAL); iBtn.setMinimumWidth(100);//I set 100px for minimunWidth. iBtn.setWidth(width); iBtn.setText(Integer.toString(i + 1)); iBtn.setId(i + 1); row.addView(iBtn, 4 + i - j); i++; } add_button_particiapants.addView(row); } } } 

我遇到了这个问题,在UI上放置的视图数量是在运行时设置的。 所以我决定我只需要一行四个视图,并相应地修改上面的代码。 另外,我总是用不可见的编辑文本填充最后一行,以便最后一行中的视图也与上一行中的视图具有相同的宽度:

  // 1.0f is the weight! LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); layoutParams.setMarginEnd(10); for (int i = 0; i < Math.ceil(discipline.getSeries_count() / 4.0); i++) { LinearLayout layoutRow = new LinearLayout(MyActivity.this); layoutRow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); // add only four edit texts to row and move to next line afterwards for (int j = 0; j < 4; j++) { etSeries = new EditText(MyActivity.this); etSeries.setInputType(InputType.TYPE_CLASS_NUMBER); int iIndex = (j + 1 + (i * 4)); etSeries.setText(getResources().getString(R.string.series) + " " + iIndex); etSeries.setId(iIndex); etSeries.setId(i); etSeries.setSelectAllOnFocus(true); etSeries.setLayoutParams(layoutParams); etSeries.setVisibility(iIndex > discipline.getSeries_count() ? View.INVISIBLE : View.VISIBLE); layoutRow.addView(etSeries); } layoutSeries.addView(layoutRow); }