【问题标题】:Android: change width in Table Layout using LayoutParams so the content can be fitAndroid:使用 LayoutParams 更改表格布局中的宽度,以便内容适合
【发布时间】:2014-08-27 03:05:04
【问题描述】:

所以我正在创建一个表格,其中包含非常长的 4-5 列文本,我需要的是能够在每列(宽度和高度)中包装内容的表格。我希望我的所有内容都能被看到。如果文本很长,表格会自动将内容放到下一行。 (现在,它仍然填满了整个模拟器宽度,所以看不到其他列)

这是我当前在 MainActivity.java 的代码:

int rows = c.getCount();
int cols = c.getColumnCount();

c.moveToFirst();

// outer for loop
for (int i = 0; i < rows; i++) 
{
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

// inner for loop
for (int j = 0; j < cols; j++) 
{
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.LEFT);
tv.setTextSize(10);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));

row.addView(tv);                
}
c.moveToNext();
mytable.addView(row);

}

如果文本很长,我想要的是这样的:

id     description       name 
1000   the quick brown   John
       fox jumps over    Doe
       the dog

但是我现在的是这样的

id     description       name 
1000   the quick brown fox jumps over the dog

无法看到名称字段,因为第 2 列的文本很长。请帮我解决这个问题。谢谢。。

【问题讨论】:

    标签: android background-color android-tablelayout layoutparams


    【解决方案1】:

    您可以尝试将 tablerow 分成相等的部分 链接:Set equal width of columns in table layout in Android

    您可以设置 textview 的宽度或 textview 的最大宽度(setWidth(), setMaxWidth()),这样您的 textview 将在超出设置宽度时变为多行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2012-07-01
      • 2014-06-02
      • 2018-04-27
      • 2012-03-26
      • 2012-01-24
      • 2013-11-30
      相关资源
      最近更新 更多