在 xml 中将 TableRow 动态添加到 TableLayout
Adding TableRow Dynamically to TableLayout in xml
虽然这看起来重复了很多问题,但对我来说还是第一次。我找遍了也没有得到结果,最后在这里发帖。
我正在动态创建一个 table,其中 TableLayout
部分写在 xml 部分中。
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="@+id/tl_componentA">
</TableLayout>
</RelativeLayout>
我为 table
创建了一个对象
TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
现在我尝试从这里开始动态添加一行
createTableRowColumn(tableLayoutA);
相关函数是
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
我做了所有这些,但模拟器中没有显示任何内容。但是,当我在 xml 中手动给出相同的结构时,一切正常。
出于这个原因,我尝试了一些事情来弄清楚
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
在 toast 中显示 0。我不明白为什么,尽管我已经在 xml 本身中固定了 tableLayoutA 的大小。
一个table在现实生活中至少需要一行一列。
您没有看到 table,因为您只创建了一行,但是没有列。
您需要在行中添加一列才能显示某些内容。
试试这个:
TextView label_date = new TextView(this);
label_date.setText("DATE");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tableRow.addView(label_date);// add the column to the table row here
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
您可以将 textView 替换为您想要的任何值。
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
这里发生的事情是,你说,在 tableLayout 中添加一个视图,你告诉它是哪个视图,你还说视图的宽度和高度应该包裹它的内容。
此外,指定 40,70 不是一个好主意,当您有不同的屏幕尺寸时会发生什么。此外,使用库来处理动态视图的添加和删除。您无需重新发明轮子并节省大量时间和精力。对于 table 的观看次数,https://github.com/EsotericSoftware/tablelayout 可能是一个不错的观看次数(不确定)。另一个问题是,table 查看您要找的是什么?确保您没有将 recyclerView 误认为是 table 视图,我这样说是因为我不理解您的用例。
有用link:https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/
虽然这看起来重复了很多问题,但对我来说还是第一次。我找遍了也没有得到结果,最后在这里发帖。
我正在动态创建一个 table,其中 TableLayout
部分写在 xml 部分中。
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="@+id/tl_componentA">
</TableLayout>
</RelativeLayout>
我为 table
创建了一个对象TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
现在我尝试从这里开始动态添加一行
createTableRowColumn(tableLayoutA);
相关函数是
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
我做了所有这些,但模拟器中没有显示任何内容。但是,当我在 xml 中手动给出相同的结构时,一切正常。
出于这个原因,我尝试了一些事情来弄清楚
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
在 toast 中显示 0。我不明白为什么,尽管我已经在 xml 本身中固定了 tableLayoutA 的大小。
一个table在现实生活中至少需要一行一列。 您没有看到 table,因为您只创建了一行,但是没有列。
您需要在行中添加一列才能显示某些内容。
试试这个:
TextView label_date = new TextView(this);
label_date.setText("DATE");
label_date.setTextColor(Color.WHITE);
label_date.setPadding(5, 5, 5, 5);
tableRow.addView(label_date);// add the column to the table row here
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
您可以将 textView 替换为您想要的任何值。
tableLayout.addView(tableRow, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
这里发生的事情是,你说,在 tableLayout 中添加一个视图,你告诉它是哪个视图,你还说视图的宽度和高度应该包裹它的内容。
此外,指定 40,70 不是一个好主意,当您有不同的屏幕尺寸时会发生什么。此外,使用库来处理动态视图的添加和删除。您无需重新发明轮子并节省大量时间和精力。对于 table 的观看次数,https://github.com/EsotericSoftware/tablelayout 可能是一个不错的观看次数(不确定)。另一个问题是,table 查看您要找的是什么?确保您没有将 recyclerView 误认为是 table 视图,我这样说是因为我不理解您的用例。
有用link:https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/