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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2684775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:19  来源:igfitidea点击:

android: two issues using Tablerow+TextView in Tablelayout

androidtextviewtablelayouttablerow

提问by Yang

I am using Tablerow+TextView to make a simple view for blog posts and their replies. In each TableRow I put a TextView in. Now I have two issues:

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

  1. The text which is longer than the screen won't automatically wrap up to be multi-line. Is it by design of TableRow? I've already set tr_content.setSingleLine(false);[update] This has been addressed, I think I should change Fill_parent to be Wrap_content in textView.tr_author_time.setLayoutParams(new LayoutParams( LayoutParams.**WRAP_CONTENT**, LayoutParams.WRAP_CONTENT));

  2. The Table won't scroll like ListView. My rows are more than the screen size. I expect the table could be scrolled down for viewing just like ListView. Is that possible?

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

  2. 表格不会像 ListView 那样滚动。我的行超过屏幕大小。我希望可以像 ListView 一样向下滚动表格以进行查看。那可能吗?

Here is my code:

这是我的代码:

    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));

    }   

回答by Sharique Abdullah

A more appropriate thing to do for wrapping items would have been to add android:shrinkColumns="*" or android:shrinkColumns="1" to the TableLayout, this would probably have fixed the wrapping issue.

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

For Details

详情

回答by synic

This isn't really a complete answer, but it really seems like you're doing this the hard way.

这并不是一个完整的答案,但看起来你真的很难做到这一点。

Instead of constructing your TableRows manually, you should set them up in xml like this:

您应该像这样在 xml 中设置它们,而不是手动构建您的 TableRows:

tablerow.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>

Prior to your loop, get a reference to a LayoutInflater:

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

LayoutInflater inflater = getLayoutInflater();

Then, inside your loop, create an instance of tablerow using the LayoutInflater:

然后,在您的循环中,使用 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);

This will allow you to set your layout, appearance, layout params in xml making it much easier to read and debug.

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

For the scrolling problem, you'll need to add your TableLayout to a ScrollView. Something like this in your xml:

对于滚动问题,您需要将 TableLayout 添加到 ScrollView。在你的 xml 中是这样的:

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

回答by John

To wrap text in table rows:

要在表格行中换行:

By default, TableLayout rows fit the width of their content, no matter it goes over the screen bounds. To get the wider-than-screen text cells to wrap to multi-line, use android:shrinkColumnsattribute on TableLayout.

默认情况下,TableLayout 行适合其内容的宽度,无论它是否超出屏幕边界。要使比屏幕更宽的文本单元格换行为多行,请使用 android:shrinkColumnsTableLayout 上的属性。

<TableLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*" />

android:shrinkColumnsis zero-based index of the columns to shrink. It removes unnecessary extra space from a column and shrinks it :

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

  • android:shrinkColumns="*"shrinks all columns
  • android:shrinkColumns="0"shrinks first column
  • android:shrinkColumns="1,2"shrinks the second and third columns
  • android:shrinkColumns="*"缩小所有列
  • android:shrinkColumns="0"缩小第一列
  • android:shrinkColumns="1,2"缩小第二列和第三列

android:stretchColumnsdoes the opposite. It stretches a column to the maximum available width.

android:stretchColumns相反。它将一列拉伸到最大可用宽度。

Both "shrink" and "stretch" consider all rows of the table to compute space.

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

To scroll down a TableLayout:

要向下滚动 TableLayout:

If your TableLayoutis higher than the screen, move it in a ScrollView.

如果您TableLayout的高度高于屏幕,请将其移动到ScrollView.