android:使用Tablerow的两个问题+;Tablelayout中的文本视图

android:使用Tablerow的两个问题+;Tablelayout中的文本视图,android,textview,tablelayout,tablerow,Android,Textview,Tablelayout,Tablerow,我正在使用Tablerow+TextView为博客文章及其回复创建一个简单的视图。在每个表格行中,我都放了一个文本视图。现在我有两个问题: 比屏幕长的文本不会自动换行为多行。这是TableRow的设计吗?我已经设置了tru content.setSingleLine(false)[update]这已经解决了,我想我应该在textView中将Fill\u parent更改为Wrap\u content。tr\u author\u time.setLayoutParams(新的LayoutParam

我正在使用Tablerow+TextView为博客文章及其回复创建一个简单的视图。在每个表格行中,我都放了一个文本视图。现在我有两个问题:

  • 比屏幕长的文本不会自动换行为多行。这是TableRow的设计吗?我已经设置了
    tru content.setSingleLine(false)[update]这已经解决了,我想我应该在textView中将Fill\u parent更改为Wrap\u content。
    tr\u author\u time.setLayoutParams(新的LayoutParams(
    LayoutParams.*包装内容**,
    LayoutParams.WRAP_内容)

  • 该表不会像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));
    
        }   
    
    TableLayout tl=(TableLayout)findViewById(R.id.article\u content\u table);
    TextView tr_title=新的TextView(本);
    TextView tr_author_time=新的TextView(此);
    TextView tr_content=新的TextView(此);
    TableRow tr=新的TableRow(本);
    对于(inti=0;i
    这并不是一个完整的答案,但看起来你做得很艰难

    您不应该手动构建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>
    
    然后,在循环中,使用LayoutInflater创建tablerow的实例:

    LayoutInflater inflater = getLayoutInflater();
    
    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);
    
    这将允许您在xml中设置布局、外观和布局参数,使其更易于阅读和调试

    对于滚动问题,您需要将TableLayout添加到滚动视图中。类似于xml中的内容:

    <ScrollView>
        <TableLayout android:id="@+id/arcitle_content_table" />
    </ScrollView>
    

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

    要将文本换行到表格行中,请执行以下操作: 默认情况下,TableLayout行适合其内容的宽度,无论其是否超出屏幕边界。若要将比屏幕更宽的文本单元格换行,请在TableLayout上使用
    android:shrinkColumns
    属性

    <TableLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*" />
    
    
    
    是要收缩的列的从零开始的索引。它从列中删除不必要的额外空间并将其收缩:

    • android:shrinkColumns=“*”
      收缩所有列
    • android:shrinkColumns=“0”
      收缩第一列
    • android:shrinkColumns=“1,2”
      收缩第二列和第三列
    相反,它将列拉伸到最大可用宽度

    “收缩”和“拉伸”都考虑表的所有行来计算空间。

    要向下滚动表格布局,请执行以下操作:
    如果您的
    TableLayout
    高于屏幕,请在
    ScrollView

    中移动它。似乎无法添加到中。程序错误退出。事实上,我看到只有视图可以添加到ScrollView中,但不能添加到布局。TableLayout是视图的后代。它应该可以正常工作。错误是什么?有效。to向ScrollView添加宽度和高度属性。谢谢synic!@synic,请用完整代码编辑tablerow.xml文件。我遇到一些错误,不知道应该在该布局中添加更多内容。出现问题!tablerow=(tablerow)充气器。充气(R.id.tablerow,tl,false);tablerow=(tablerow)充气器。充气(R.layout.tablerow,tl,false);它肯定是OK+1。我遇到了类似的问题,这是最简单/更可靠的修复方法。谢谢!我们如何使两行高度相等?请查看我的问题如何使两行高度相等?请查看我的问题