以编程方式填充的TableLayout将不会显示在片段中

时间:2014-06-12 22:57:31

标签: android android-fragments android-tablelayout

尝试:从一个空的表格布局开始,然后用行动态地增长它。

问题:我什么都没看到。我已经检查过,onCreateView确实被调用但仍然没有显示。

注意:我正在使用片段

片段类:

 public class PointsTableFragment extends Fragment {

    String[][] data = { 
            { "team", "p1", "p2", "p3", "p4", "p5", "p6", "p7" },
            { "team", "p1", "p2", "p3", "p4", "p5", "p6", "p7" },
            { "team", "p1", "p2", "p3", "p4", "p5", "p6", "p7" },
            { "team", "p1", "p2", "p3", "p4", "p5", "p6", "p7" },
            { "team", "p1", "p2", "p3", "p4", "p5", "p6", "p7" }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.fragment_points_table,
                container, false);

        TableLayout table = (TableLayout) rootView
                .findViewById(R.id.points_table);



        for (String[] row : data) {

            TableRow tr = new TableRow(this.getActivity());
            tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));

            for(String value : row){

                TextView tv = new TextView(this.getActivity());
                tv.setText(value);
                tv.setTextSize(18);
                tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                tv.setTextColor(Color.RED);
                tr.addView(tv);

            }   
            table.addView(tr);
        }



        return rootView;
    }
    }

片段布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fixture_list_divider"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/points_table"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="0,1,2,3,4,5,6,7" >
    </TableLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您需要使用LinearLayout LayoutParamsTableLayout LayoutParams作为参数而非本地ViewGroup LayoutParams

改变这一点:

    tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));