自TableLayout与行多TextViews [英] Custom TableLayout with multiple TextViews in rows

查看:131
本文介绍了自TableLayout与行多TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想箱子定制TableLayout用这样的行:

I want to crate custom TableLayout with rows like this:

电视是TextView的,即我要添加11 TextViews到该行:

TV is for TextView, i.e. I want to add 11 TextViews to the row:

每行开头的标题,然后我想补充5对TextViews,使表行是宽,因为画面。 这是我的code:

Each row begins with a title and then I add 5 pairs of TextViews, so that table row is as wide, as screen is. Here's my code:

public class FlowTable extends TableLayout {

    private Context context;

    public FlowTable(Context context) {
        super(context);
        this.context = context;
    }

    public FlowTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void addContent(List<ResultItem> data) {

        TableRow tableRow = new TableRow(context);

        LayoutParams params = new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);

        for (int i = 0; i < data.size(); i++) {

            if (i % 5 == 0) {
                this.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                tableRow = new TableRow(context);
                TextView tvRange = new TextView(context);
                tvRange.setLayoutParams(params);
                tvRange.setText(genRange(i+1));
                tableRow.addView(tvRange);

            }
            TextView tvDistance = new TextView(context);
            tvDistance.setLayoutParams(params);
            tvDistance.setText(String.valueOf(data.get(i).distance));

            TextView tvResult = new TextView(context);
            tvResult.setLayoutParams(params);
            tvResult.setText(data.get(i).result);

            tableRow.addView(tvDistance);
            tableRow.addView(tvResult);
        }
    }

    private String genRange(int currIndex){
        /********************/
        return somestring;
    }
}

使用表:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <packagename.FlowTable
        android:id="@+id/flowTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

在片段:

View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
        FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
        flowTable.addContent(data);

的问题:屏幕仅仅是空的!什么都没有。在我加入的布局PARAMS到TextView的它的工作,但排不占用屏幕宽度。我最初的解决方案是基于的LinearLayout样本,因为的TableRow是的LinearLayout的延伸。但我不能使它发挥作用。 谢谢你。

The problem: the screen is just empty! Nothing at all. Before I added the layout params to textview it worked, but row didn't occupy the screen width. My initial solution was based on the LinearLayout samples, because TableRow is an extention of LinearLayout. But I can't make it work. Thanks.

推荐答案

尝试以编程方式设置的所有列伸展(似乎并没有在XML中为我工作):

Try programmatically setting all columns to stretch (didn't seem to work in XML for me):

...
flowTable.addContent(data);
flowTable.setStretchAllColumns(true);

其他一些快速的事实:

Some other quick facts:

  • 没有必要去尝试,并指定高度和宽度作为内TableLayout的TableRow ,因为它总是会高度= WRAP_CONTENT和宽度= MATCH_PARENT。看到此列在类概述部分 TableLayout文档
  • 没有必要去尝试,并指定高度和窗口小部件作为的TableRow儿童,因为他们永远是高度= WRAP_CONTENT和宽度= MATCH_PARENT。看到此列在类概述部分的TableRow文档
  • No need to try and specify height and width for TableRow within TableLayout because it will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableLayout documentation where this is listed in the Class Overview section
  • No need to try and specify height and widget for children of TableRow because they will always be height=WRAP_CONTENT and width=MATCH_PARENT. See TableRow documentation where this is listed in the Class Overview section

也许我也虚心地建议了一下重构的:

Might I also humbly suggest a bit of refactoring:

public class FlowTable extends TableLayout {
    private TableRow mCurrentRow;

    public FlowTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FlowTable(Context context) {
        super(context);
        init();
    }

    private void init() {
        mCurrentRow = new TableRow(getContext());
        mCurrentRow.addView(createAndFillTextView("0")); // title for first row
        setStretchAllColumns(true);
    }

    public void addContent(List<ResultInfo> data) {
        for (int i = 0; i < data.size(); i++) {
            if ((i % 5 == 0) && (i != 0) /** Don't do this on 0! */) {
                finishRowAndStartNew(i);
            }

            mCurrentRow.addView(createAndFillTextView(data.get(i).distance));
            mCurrentRow.addView(createAndFillTextView(data.get(i).result));
        }
    }

    private void finishRowAndStartNew(int newRowIndex) {
        addView(mCurrentRow);
        mCurrentRow = new TableRow(getContext());
        mCurrentRow.addView(createAndFillTextView(genRange(newRowIndex+1)));
    }

    private String genRange(int currIndex){
        /********************/
        return String.valueOf(currIndex);
    }

    private TextView createAndFillTextView(String text) {
        TextView tv = new TextView(getContext());
        tv.setText(text);        
        return tv;
    }
}

这篇关于自TableLayout与行多TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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