java - Android 表格单元格边框

标签 java android datatable android-tablelayout

下面的 xml 代码为我的表格单元格提供了正确的边框

<TableRow android:background="#cdcdcd" > 
   <TextView android:text="1st Column" android:background="#ffffff" android:layout_margin="2dip"/>
   <TextView android:text="2nd Column" android:background="#ffffff" android:layout_margin="2dip"/>
   <TextView android:id="@+id/amount"  android:background="#ffffff"  android:layout_margin="2dip" android:text="3rd col"/>
</TableRow>

但是当我尝试以编程方式执行此操作时,它不起作用,这是我的代码:

    dataTable = (TableLayout)findViewById(R.id.data_table);             
    TableRow tr=new TableRow(this); 
    counter++;
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    lp.setMargins(2,2, 2, 2);

    TextView tv= new TextView(this);
    tv.setLayoutParams(lp);
    tv.setText("text"+counter);
    tv.setBackgroundColor(Color.parseColor("#ffffff"));             
    counter++;

    TextView cb= new TextView(this);
    cb.setLayoutParams(lp);
    cb.setText("text"+counter);
    counter++;

    TextView ib= new TextView(this);
    ib.setText("text"+counter);
    ib.setLayoutParams(lp);     

    tr.setBackgroundColor(Color.parseColor("#cdcdcd"));
    tr.setPadding(2, 2, 2, 2);
    tr.addView(tv);
    tr.addView(cb);
    tr.addView(ib);
    dataTable.addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

除了边框以及当我设置“tv.setLayoutParams(lp);”时,一切正常参数列消失。

最佳答案

尝试通过调用添加 View addView(View child, int index, ViewGroup.LayoutParams params)

此外,尝试为每个 View 创建一个新的 LayoutParams 实例

关于java - Android 表格单元格边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541095/

相关文章:

java - System.out.println - 这个方法在 Java 中是链式的吗?

java - 访问修饰符如何影响 Java 的性能?

java - android直接使用mysql-connector连接mysql数据库

java - 从基于 Android 的设备读取文件?

表格列内的 Primefaces 删除和确认对话框 - 更新或卡住问题

c# - 如何在数据表中按列名添加新行

java - CDI @Interceptor 的@Priority 的含义是什么?

java - 使用查询字符串搜索文件在 Google Drive api 上不起作用

java - 如何使用微调器更改背景图像

c# - 使用任何数据列对数据行进行排序