尝试:从一个空的表格布局开始,然后用行动态地增长它。
问题:我什么都没看到。我已经检查过,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>
答案 0 :(得分:0)
您需要使用LinearLayout LayoutParams
和TableLayout 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));