我希望我的表能够在整个屏幕上显示列和行之间的间距。 我在java中动态创建我的行,具体取决于玩家的数量。在java代码中我添加属性,以便表占据整个屏幕。但我无法实现它。下面附有我的代码和xml文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="views.controllers.RoundInfo">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addRound"
android:text="@string/addround" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="@string/roundinfo"
android:textSize="30sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/maintable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
public void addData() {
numOfPlayers = currentRound.getPlayers().size();
row = new String[numOfPlayers];
for (int i = 0; i < numOfPlayers; i++) {
row[i] = currentRound.getPlayers().get(i).getPlayer().getName();
}
col = new String[]{"Winner", "Seen", "Less", "Points"}; // get from database
int rowCount = row.length;
for (int i = 0; i <= rowCount; i++) {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tableRow.setLayoutParams(tableRowParams);
// create tableRow
for (int j = 0; j <= 4; j++) {
//create textView
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
CheckBox checkBox = new CheckBox(this);
EditText point = new EditText(this);
if (i == 0 && j == 0) {
textView.setText(" ");
tableRow.addView(textView);
} else if (i > 0 && j == 0) {
textView.setText(row[i - 1]); // Player Header
tableRow.addView(textView);
} else if (i == 0 && j != 0) {
textView.setText(col[j - 1]); //Game Header
tableRow.addView(textView);
} else if (i > 0 && j == 1) {
checkBox.setText(""); // Is Winner
tableRow.addView(checkBox);
} else if (i > 0 && j == 2) {
checkBox.setText(""); // Is Seen
tableRow.addView(checkBox);
} else if (i > 0 && j == 3) {
checkBox.setText(""); // Is Less
tableRow.addView(checkBox);
} else if (i > 0 && j == 4) {
point.setInputType(100); // Points
tableRow.addView(point);
}
}
tl.addView(tableRow);
}
}
答案 0 :(得分:0)
如果您希望您的桌子在屏幕上占用尽可能多的空间,请删除其父母的边距和填充
例如根布局如何填充,删除此
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
如果您重新考虑如何实施表格,这可以变得更容易。
在您的布局中空白TableLayout
,其中包含您希望在UI中使用的空间,
为TableRow
填充TableLayout
的单独布局,您将对LayoutInflator
行使用此单独布局,并填充所需的行数
LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
layoutInflater.inflate(R.layout.your_single_row, parent, false);
这将返回一个View对象,您可以将其添加到TableLayout
tableLayout.addView("you view will come here which you get from the inflater");