我正在使用Tablerow + TextView对博客帖子及其回复进行简单查看。在每个TableRow中,我都放置了一个TextView。现在有两个问题:
tr_content.setSingleLine(false);
[update]这个问题已经解决,我认为我应该将Fill_parent更改为textView中的Wrap_content。 tr_author_time.setLayoutParams(new LayoutParams( LayoutParams.**WRAP_CONTENT**, LayoutParams.WRAP_CONTENT));
这是我的代码:
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));
}
最佳答案
这并不是一个完整的答案,但似乎确实是您在努力地做。
除了应该手动构造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的引用:
LayoutInflater inflater = getLayoutInflater();
然后,在循环中,使用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);
这将允许您在xml中设置布局,外观,布局参数,从而使其更易于阅读和调试。
对于滚动问题,您需要将TableLayout添加到ScrollView。在您的xml中是这样的:
<ScrollView>
<TableLayout android:id="@+id/arcitle_content_table" />
</ScrollView>