android - 在行中具有多个 TextView 的自定义 TableLayout

标签 android android-layout

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

TV 用于 TextView,即我想向该行添加 11 个 TextView:

enter image description here

每一行都以标题开头,然后我添加了 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;
    }
}

使用表格:

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

在 fragment 中:

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

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

最佳答案

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

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

其他一些小知识:

  • 无需尝试为 TableLayout 中的 TableRow 指定高度和宽度,因为它始终是 height=WRAP_CONTENT 和 width=MATCH_PARENT。参见 TableLayout documentation这在类(class)概述部分中列出
  • 无需尝试为 TableRow 的子项指定高度和小部件,因为它们始终为 height=WRAP_CONTENT 和 width=MATCH_PARENT。参见 TableRow documentation这在类(class)概述部分中列出

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

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

关于android - 在行中具有多个 TextView 的自定义 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8554181/

相关文章:

java - 单击菜单后调用另一个 Activity

android - 为什么在安卓模拟器上无法使用录音机?

java - 自定义警报对话框中的访问按钮

android - 当我在手机上运行应用程序时。版本是 android 4.0.3

android - 如果太长,将 textview 推到新行

java - 在将我的相对布局更改为 Linearlayout 时,它在 TTS 的 textview 上抛出空指针异常

android - 如何以编程方式打开 Google Play 商店的 "My account"

android - 在 SIPDROID 中更改服务器

android - 使用光标但没有键盘进行文本编辑

android - 如何从其他 View 创建我自己的 XML 布局 View