机器人:两个Tablelayout使用的TableRow + TextView的问题 [英] android: two issues using Tablerow+TextView in Tablelayout

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

问题描述

我使用的TableRow + TextView中作出的博客文章和答复一个简单的观点。在每一个的TableRow我把一个TextView现在我有两个问题:

  1. 长于屏幕也不会自动包是多行的文本。它是通过的TableRow的设计?我已经设定 tr_content.setSingleLine(假); [更新]这已得到解决,我想我应该改变FILL_PARENT在TextView中被WRAP_CONTENT tr_author_time.setLayoutParams(新的LayoutParams(                     的LayoutParams ** WRAP_CONTENT **,                     LayoutParams.WRAP_CONTENT));

  2. 该表将不会像滚动的ListView。我行是超过屏幕尺寸。我预计该表可以向下滚动观看,就像ListView控件。这可能吗?

下面是我的code:

  TableLayout TL =(TableLayout)findViewById(R.id.article_content_table);
        TextView的tr_title =新的TextView(本);
    TextView的tr_author_time =新的TextView(本);
    TextView的tr_content =新的TextView(本);
    的TableRow TR =新的TableRow(本);

    的for(int i = 0; I< BlogPost.size();我++){
        尝试{
        //添加作者,时间
        TR =新的TableRow(本);
        /////////////////添加笔者+时间排
        博文本文= mBlogPost.get(ⅰ);
        tr_author_time =新的TextView(本);
        tr_author_time.setText(article.author +(+
                article.post_time +));
        tr_author_time.setTextColor(getResources()的getColor(R.color.black));
        tr_author_time.setGravity(03);
        tr_author_time.setLayoutParams(新的LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        tr.addView(tr_author_time);
        tl.addView(TR,新TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        //////////////////////然后添加内容一行
        TR =新的TableRow(本);
        tr_content =新的TextView(本);
        tr_content.setText(article.content);
        tr_content.setSingleLine(假);
        tr_content.setGravity(03);
        tr_content.setLayoutParams(新的LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        tr.addView(tr_content);
         tr.setBackgroundResource(R.color.white);
            tl.addView(TR,新TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

    }
 

解决方案

这是不是一个真正的完整的答案,但它真的好像你这样做了艰辛的道路。

而不是手动构建你的TableRows的,你应该将它们设置在XML中是这样的:

tablerow.xml:

 <的TableRow的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    < TextView的机器人:ID =@ + ID /内容
        机器人:单线=假
        机器人:textAppearance =@风格/ so​​meappearance/>
< /的TableRow>
 

在此之前你的循环,得到的引用LayoutInflater:

  LayoutInflater吹气= getLayoutInflater();
 

那么,你的循环中,使用LayoutInflater创建的TableRow实例:

 的TableRow行=(的TableRow)inflater.inflate(R.layout.tablerow,TL,假);
TextView的内容=(TextView中)row.findViewById(R.id.content);
content.setText(这是内容);

tl.addView(行);
 

这将允许您设置您的布局,外观,在XML布局PARAMS使其更容易阅读和调试。

有关滚动问题,您需要将您的TableLayout添加到滚动型。这样的事情在你的XML:

 <滚动型>
    < TableLayout机器人:ID =@ + ID / arcitle_content_table/>
< /滚动型>
 

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

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

    }   

解决方案

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:

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 inflater = getLayoutInflater();

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

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

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>

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

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