【问题标题】:Custom TableLayout with multiple TextViews in rows行中具有多个 TextView 的自定义 TableLayout
【发布时间】:2011-12-18 19:25:59
【问题描述】:

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

TV 用于 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;
    }
}

使用表格:

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

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

【问题讨论】:

  • 我不确定它是否会起作用,但请尝试将android:stretchColumns="*" 添加到您的&lt;FlowTable&gt; 标记中。否则,我也会尝试使用另一个 addView 重载:tableRow.addView(tvRange, params)

标签: android android-layout


【解决方案1】:

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

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

其他一些简单的事实:

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

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

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

【讨论】:

  • 谢谢!我删除了参数并添加了 flowTable.setStretchAllColumns(true);现在可以使用了!
【解决方案2】:

如果您想为此使用 xml 布局,请尝试以下操作:

    <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TableRow android:weightSum="1">
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText"
                            android:layout_height="wrap_content" android:layout_width="wrap_content"
                            android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                            android:text="Title" android:layout_weight="1"/>
                    </LinearLayout>
                    <!-- First Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                    <!-- Second Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>


                    <!-- Third Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>

                    <!-- Fourth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2" />
                    </LinearLayout>
                    <!-- Fifth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">

                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                </TableRow>
                <TableRow android:weightSum="1">
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText"
                            android:layout_height="wrap_content" android:layout_width="wrap_content"
                            android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                            android:text="Title" android:layout_weight="1"/>
                    </LinearLayout>
                    <!-- First Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                    <!-- Second Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>


                    <!-- Third Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>

                    <!-- Fourth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="4dp">

                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2" />
                    </LinearLayout>
                    <!-- Fifth Group -->
                    <LinearLayout android:layout_height="wrap_content" android:orientation="horizontal"
                        android:layout_width="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp">

                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv1"/>
                        <TextView android:id="@+id/totalText" 
                                android:layout_height="wrap_content" android:layout_width="wrap_content"
                                android:textStyle="bold" android:textSize="12sp" android:textColor="#000000"
                                android:text="Tv2"/>
                    </LinearLayout>
                </TableRow>
</TableLayout>

希望对您有所帮助。 . . 如果你想要其他然后xml然后让我知道。 谢谢。

【讨论】:

  • 谢谢,我会尝试将其转换为程序化布局创建。
  • 是的,当然。如果你没有得到想要的输出,请告诉我。
猜你喜欢
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2018-07-27
  • 2012-10-02
  • 2015-02-28
相关资源
最近更新 更多