【问题标题】:Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layoutAndroid - 使用现有的 TableRow 布局将 TableRow 动态添加到 TableLayout
【发布时间】:2023-03-18 15:18:01
【问题描述】:

我正在尝试将 TableRows 添加到 TableLayout 中,这非常有效。但是,我在布局参数方面遇到了一些问题。 TableRow 中 TextView 之间的间距没有像我想象的那样工作。

我当前的代码如下所示。

paymentTable = (TableLayout) findViewById(R.id.paymentTable);

for(i = 0; i < cursor.getCount(); i++) {

        TableRow row = new TableRow(this);

        TextView payAmount = new TextView(this);
        payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
        payAmount.setPadding(payAmount.getPaddingLeft(), 
                             payAmount.getPaddingTop(), 
                             textView.getPaddingRight(), 
                             payAmount.getPaddingBottom());

        TextView payDate = new TextView(this);
        payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));

        row.addView(payDate);
        row.addView(payAmount);

        paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        cursor.moveToNext();
    }

之后,TableLayout 看起来类似于:

January$500

February$500

March$405

但我希望它看起来像:

January    $500
February   $500
March      $405

为了澄清事情,我希望每个新的 TableRow 和它包含的 TextViews 继承现有 TableRow 和 TextView(s) 的布局属性。

【问题讨论】:

    标签: android tablelayout android-tablelayout


    【解决方案1】:

    你需要为每个Table添加一个RelativeLayout

    TableRow row = new TableRow(this);
                                row.setId(tag);
                             // Creating a new RelativeLayout
    
                                RelativeLayout relativeLayout = new RelativeLayout(this);
                                // as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
                                TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                                        TableRow.LayoutParams.MATCH_PARENT,
                                        TableRow.LayoutParams.MATCH_PARENT);   
                                rlp.setMargins(0, 0, 0, 0);
                                row.addView(relativeLayout, rlp);
    

    然后将 TextView 添加到 RelativeLayout。像这样

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                              RelativeLayout.LayoutParams.WRAP_CONTENT,
                              RelativeLayout.LayoutParams.WRAP_CONTENT);
    TextView payAmount = new TextView(this);
    //lp.setMargins(left, top, right, bottom)
    lp.setMargins(0, 0, 16, 0);
    lp.addRule(RelativeLayout.ALIGN_LEFT);
    payAmount.setLayoutParams(lp);
    relativeLayout.addView(payAmount);
    
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                              RelativeLayout.LayoutParams.WRAP_CONTENT,
                              RelativeLayout.LayoutParams.WRAP_CONTENT);
    TextView payDate = new TextView(this);
    //lp.setMargins(left, top, right, bottom)
    lp.setMargins(0, 0, 16, 0);
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    payDate.setLayoutParams(lp);
    relativeLayout.addView(payDate);
    

    这是我处理在表格行中定位界面组件的方式。

    【讨论】:

    • 我如何设置相对布局的方向,我在循环中添加文本视图,然后将所有文本视图添加到相对布局,但是所有这些文本视图都是水平添加的,新手请告诉我如何实现我的目标?
    【解决方案2】:

    yawus 这是一个很好的教程,将帮助您设置表格布局 http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

    这是xml布局

    http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/

    这是活动代码

    http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/

    TableRow tableRow= new TableRow(this);
    
                ArrayList<Object> row = data.get(position);
    
                TextView idText = new TextView(this);
                idText.setText(row.get(0).toString());
                tableRow.addView(idText);
    
                TextView textOne = new TextView(this);
                textOne.setText(row.get(1).toString());
                tableRow.addView(textOne);
    
                TextView textTwo = new TextView(this);
                textTwo.setText(row.get(2).toString());
                tableRow.addView(textTwo);
    
                dataTable.addView(tableRow);
    

    【讨论】:

    • 我决定下载他的代码并自己运行。但是,似乎他自己也遇到了同样的问题。虽然他为列设置的最小宽度有所帮助,但“TextFieldOne”和“TextFieldTwo”的列仍然在没有间距的情况下一起运行。
    • 看看这个如果你有同样的问题然后你可以参考如果没有然后我们可以试试别的stackoverflow.com/questions/6660849/…
    • 在运行时调整填充可以使用 tableRow.setPadding(5, 0, 0, 0);
    • Yawus 你在为表格的每个单元格使用一些背景图片
    • @Yawus 如果您使用背景图片,那么使用带边框的图片会是更好的选择
    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多