Tablerow和TableLayout未对齐



data_shifted_down

我正在创建一个android项目(java),它从服务器中提取数据并将数据放入表中,因此我必须以编程方式创建一个表。我在这里举了几个例子,但对于行中的每一列,它都被下移。。。我附上了一张照片。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:id="@+id/details_table"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<TableRow>
<TextView
android:text="User"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textSize="50px"
android:textColor="@color/black"  />
<TextView
android:text="Store"
android:layout_width="match_parent"
android:layout_column="1"
android:layout_weight="1"
android:textSize="50px"
android:textColor="@color/black"  />
<TextView
android:text="Item"
android:layout_width="match_parent"
android:layout_column="2"
android:layout_weight="1"
android:textSize="50px"
android:textColor="@color/black" />
<TextView
android:text="QTY"
android:layout_width="match_parent"
android:layout_column="3"
android:layout_weight="1"
android:textSize="50px"
android:textColor="@color/black"
/>
</TableRow>
</TableLayout >
</LinearLayout>

table_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow
android:id="@+id/tr"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/tableCell1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/tableCell2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_toRightOf="@+id/tableCell1"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/tableCell3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_toRightOf="@+id/tableCell2"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/tableCell4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="3"
android:layout_toRightOf="@+id/tableCell3"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="20dp" />
</TableRow>
</RelativeLayout>

MainActivity.java,其中它遍历要添加到TableRow的数据,然后将该TableRow添加到TableLayout:

for (int c = 0; c < a.length; c++) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
View v=getLayoutInflater().inflate(R.layout.tablerow, null);
TableRow tableRow = (TableRow) v.findViewById(R.id.tr);
detailsTable.setStretchAllColumns(true);
TextView tv = null;
switch (c) {
case 0:
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
break;
case 1:
tv = (TextView) tableRow.findViewById(R.id.tableCell2);
break;
case 2:
tv = (TextView) tableRow.findViewById(R.id.tableCell3);
break;
case 3:
tv = (TextView) tableRow.findViewById(R.id.tableCell4);
break;
}
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText(a[c]);
tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT));
//new Log("here: ", Integer.toString(tv.getId()));
//Add row to the table
if(tableRow.getParent() !=null){
((ViewGroup)tableRow.getParent()).removeView(tableRow);
}
detailsTable.addView(tableRow);

尝试了现场的所有解决方案。更改为RelativeLayout没有运气。尝试重力和其他所有属性。。。

尝试这样做看起来很糟糕,但您的问题是,您正在为每个数据项膨胀一个新的表行:-

View v=getLayoutInflater().inflate(R.layout.tablerow, null);

谁的父母是空

因此:-

if(tableRow.getParent() !=null){ ((ViewGroup)tableRow.getParent()).removeView(tableRow); }

不会删除视图(不是说你应该删除视图)

未经测试,但类似的方法可能有效,但仅适用于a有4个数据项的情况(因为您切换语句的时间更长-也可以使用模数计算)

View v;
for (int c = 0; c < a.length; c++) {
// Every 4th data item (remainder is zero) e.g. 0,4,8,etc
if ((c%4) == 0) {
// inflate the view
v=getLayoutInflater().inflate(R.layout.tablerow, null);
}
// Rest of your code
....
//Add row to the table
if ((c%4) == 0) {
detailsTable.addView(tableRow);
}
}

最新更新