android:在Tablelayout中使用Tablerow+TextView的两个问题 [英] android: two issues using Tablerow+TextView in Tablelayout

查看:24
本文介绍了android:在Tablelayout中使用Tablerow+TextView的两个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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

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?

这是我的代码:

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

    }   

推荐答案

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

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

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

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

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>

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

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

LayoutInflater inflater = getLayoutInflater();

然后,在您的循环中,使用 LayoutInflater 创建一个 tablerow 实例:

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

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 中设置布局、外观、布局参数,使其更易于阅读和调试.

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

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

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

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

这篇关于android:在Tablelayout中使用Tablerow+TextView的两个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆