自定义TableLayout,行中有多个TextView

Igo*_*pov 4 android android-layout

我想用这样的行来创建自定义TableLayout:

电视用于TextView,即我想在行中添加11个TextView:

在此输入图像描述

每行以标题开头,然后我添加5对TextView,这样表格行就像屏幕一样宽. 这是我的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用表格:

<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>
Run Code Online (Sandbox Code Playgroud)

在片段中:

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

问题:屏幕只是空的!什么都没有.在我将布局参数添加到textview之前,它有效,但行没有占用屏幕宽度.我的初始解决方案基于LinearLayout示例,因为TableRow是LinearLayout的扩展.但我不能让它发挥作用.谢谢.

dst*_*cks 10

尝试以编程方式将所有列设置为拉伸(对我来说似乎不适用于XML):

...
flowTable.addContent(data);
flowTable.setStretchAllColumns(true);
Run Code Online (Sandbox Code Playgroud)

其他一些快速事实:

  • 无需尝试在TableLayout中指定TableRow的高度和宽度,因为它始终是height = WRAP_CONTENT和width = MATCH_PARENT.请参阅TableLayout文档,其中列出了类概述部分
  • 无需尝试为TableRow的子项指定高度和小部件,因为它们始终为height = WRAP_CONTENT和width = MATCH_PARENT.请参阅TableRow文档,其中列出了类概述部分

可能我也谦虚地建议一些重构:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)