【发布时间】:2017-09-23 17:32:54
【问题描述】:
TableLayout relativeLayout = (TableLayout)findViewById(R.id.ll1);
TableRow row =new TableRow(this);
Button button ;
int counter = 0;
for(int i = 0;i<=13;i++){
counter++;
button = new Button(this);
button.setText(counter);
row.addView(button);
}
relativeLayout.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
relativeLayout.computeScroll();
XML 文件
<TableLayout android:id="@+id/ll1" android:shrinkColumns="true"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button1"
xmlns:android="http://schemas.android.com/apk/res/android"></TableLayout>
但是每当我执行我的应用程序时,它只会将我的按钮移动到一行中,并且在 5 个按钮之后,所有按钮都会从屏幕上移出。我不想添加更多行,因为我不想在我的代码中硬编码任何东西,比如放置一些代码 if (counter%5==0) 然后做这个和那个。
是否有任何其他方法来计算宽度,如果按钮等于屏幕宽度,则执行此或那个或任何其他方式,如表格布局中的任何属性,只需包装一行的内容?
【问题讨论】:
-
如果您只有一行,请使用 LinearLayout!我不明白你到底想做什么?当您将按钮添加到布局时会发生什么,它们应该如何出现在屏幕上 - 单行,多行???
标签: android layout tablelayout