【问题标题】:Can't add TableRow to TableLayout in android无法在 android 中将 TableRow 添加到 TableLayout
【发布时间】:2018-11-07 00:35:36
【问题描述】:
TableLayout mTableLayout;
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mTableLayout = findViewById(R.id.table_layout);
mTableLayout = new TableLayout(this);

ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.black);
TableRow tableRow = new TableRow(this);
tableRow.addView(imageView);
mTableLayout.addView(tableRow);

我想将图像视图添加到表格行,然后将此表格行添加到表格布局。 但是当这个运行时,屏幕什么也没有发生。我不知道为什么。 我试图找到 Logcat 和断点,但没有错。

【问题讨论】:

    标签: android tablelayout tablerow


    【解决方案1】:
    Ok the issue here is you are creating row first the the table layout; try adding row to layout first then view to row like below reference code:
    
       TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
       TableRow row = new TableRow(this);
       TextView tv = new TextView(this);
       tv.setText("This is text");
    
       tl.addView(row);
       row.addView(tv);
    

    【讨论】:

    • 对不起。我试着照你说的做,但没有任何改变。
    • 然后通过xml试试
    • 对不起。我不能在 XML 中做到这一点。这样做的目的是我想将许多 imageView 添加到 1 行,然后通过代码将该行添加到 Table Layout。所以我不能在 XML 中做那件事。
    • 改用网格布局
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多