我正在创建一个将动态创建 tableLayout 的 fragment ,但我遇到了问题,因为它没有显示。有人可以告诉我为什么它不显示吗?除表格外,部分标签可以显示。
这是我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_fragment_bp_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="@+id/section_bp_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="@+id/tableLayoutBloodPressure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
这是我的 fragment 类——数据仅供测试
public class BloodPressureFragment extends Fragment {
public BloodPressureFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bp_list, container,
false);
TextView txt = (TextView) rootView.findViewById(R.id.section_bp_label);
txt.setText("Blood Pressure History Logs");
TableLayout ll = (TableLayout) rootView
.findViewById(R.id.tableLayoutBloodPressure);
for (int i = 1; i < 10; i++) {
TableRow tbrow = new TableRow(getActivity().getApplicationContext());
tbrow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv1 = new TextView(getActivity().getApplicationContext());
tv1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setId(i);
tv1.setText("Test id: " + i);
tbrow.addView(tv1);
ll.addView(tbrow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
return rootView;
}
}
最佳答案
我有同样的问题,我的解决方案是从 fragment View 中扩充行布局。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_intro_a,container,false); // infalte normally
Tablelayout mytable = (TableLayout) v.findViewById(R.id.mytable); // init table
/* your stuff*/
/* now fill table layout directly on fragment */
TableRow row = new TableRow(getActivity()); // new row
View table_row_view = v.inflate(getActivity(),R.layout.your_layout,row); // inflate from parent view;
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView mytextview = (TextView)vvv.findViewById(R.id.yourtextview-in-yourlayout);
mytable.addView(row, new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
/* TADAAN! rows is visible! */
关于java - Fragment 中的 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289427/