android:在Tablelayout中使用Tablerow + TextView的两个问题

Yan*_*ang 8 android tablelayout textview tablerow

我正在使用Tablerow + TextView为博客文章及其回复制作一个简单的视图.在每个TableRow中我都放了一个TextView.现在我有两个问题:

  1. 比屏幕长的文本不会自动换行为多行.是TableRow的设计吗?我已经设置了tr_content.setSingleLine(false);[更新]这已经解决,我想我应该在textView中将Fill_parent更改为Wrap_content.tr_author_time.setLayoutParams(new LayoutParams( LayoutParams.**WRAP_CONTENT**, LayoutParams.WRAP_CONTENT));

  2. 表格不会像ListView一样滚动.我的行数大于屏幕大小.我希望该表可以向下滚动以便像ListView一样查看.那可能吗?

这是我的代码:

    TableLayout tl = (TableLayout) findViewById(R.id.article_content_table);
        TextView tr_title = new TextView(this);
    TextView tr_author_time = new TextView(this);
    TextView tr_content = new TextView(this);
    TableRow tr = new TableRow(this);

    for(int i = 0; i < BlogPost.size(); i++){
        try{
        // add the author, time
        tr = new TableRow(this);
        /////////////////add author+time row
        BlogPost article = mBlogPost.get(i);
        tr_author_time = new TextView(this);
        tr_author_time.setText(article.author+"("+
                article.post_time+")");
        tr_author_time.setTextColor(getResources().getColor(R.color.black));
        tr_author_time.setGravity(0x03);
        tr_author_time.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT)); 
        tr.addView(tr_author_time); 
        tl.addView(tr,new TableLayout.LayoutParams( 
                LayoutParams.FILL_PARENT, 
                LayoutParams.WRAP_CONTENT));
        ////////////////////// then add content row
        tr = new TableRow(this);            
        tr_content = new TextView(this);
        tr_content.setText(article.content);
        tr_content.setSingleLine(false);
        tr_content.setGravity(0x03);
        tr_content.setLayoutParams(new LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));            
        tr.addView(tr_content);       
         tr.setBackgroundResource(R.color.white);
            tl.addView(tr,new TableLayout.LayoutParams( 
                    LayoutParams.FILL_PARENT, 
                    LayoutParams.WRAP_CONTENT));

    }   
Run Code Online (Sandbox Code Playgroud)

Sha*_*lah 17

包装项目更合适的做法是将android:shrinkColumns ="*"或android:shrinkColumns ="1"添加到TableLayout,这可能已经解决了包装问题.

详情


syn*_*nic 14

这不是一个完整的答案,但看起来你真的很难这样做.

而不是手动构建您的TableRows,您应该在xml中设置它们,如下所示:

tablerow.xml:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/content"
        android:singleLine="false"
        android:textAppearance="@style/someappearance" />
</TableRow>
Run Code Online (Sandbox Code Playgroud)

在循环之前,获取对LayoutInflater的引用:

LayoutInflater inflater = getLayoutInflater();
Run Code Online (Sandbox Code Playgroud)

然后,在循环内部,使用LayoutInflater创建一个tablerow实例:

TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
TextView content = (TextView)row.findViewById(R.id.content);
content.setText("this is the content");

tl.addView(row);
Run Code Online (Sandbox Code Playgroud)

这将允许您在xml中设置布局,外观,布局参数,使其更容易阅读和调试.

对于滚动问题,您需要将TableLayout添加到ScrollView.你的xml中有这样的东西:

<ScrollView>
    <TableLayout android:id="@+id/arcitle_content_table" />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)


Joh*_*ohn 6

要在表行中包装文本:

默认情况下,TableLayout行符合其内容的宽度,无论它超出屏幕范围.要使宽屏幕文本单元格换行到多行,请使用 android:shrinkColumnsTableLayout上的属性.

<TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*" />
Run Code Online (Sandbox Code Playgroud)

android:shrinkColumns是要收缩的列的从零开始的索引.它从列中删除不必要的额外空间并缩小它:

  • android:shrinkColumns="*" 收缩所有列
  • android:shrinkColumns="0" 收缩第一列
  • android:shrinkColumns="1,2" 缩小第二列和第三列

android:stretchColumns反其道而行之.它将列拉伸到最大可用宽度.

"缩小"和"拉伸"都考虑表的所有行来计算空间.

要向下滚动TableLayout:

如果您TableLayout的屏幕高于屏幕,请将其移至屏幕中ScrollView.