2013-06-25 3 views
1

Я создаю фрагмент, который будет динамически создавать tableLayout, но у меня возникают проблемы с ним, поскольку он не отображается. Может ли кто-нибудь сказать мне, почему он не отображается? Метка раздела может отображаться, за исключением таблицы.TableLayout внутри фрагмента

Вот мой 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> 

Вот мой фрагмент класса - данные только для тестирования

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

Почему вы так много 'LayoutParams()' ?! Я думаю, что это вопрос! Избавьтесь от них, не шутите, все они бесполезны. – LuckyMe

+0

Привет, спасибо за ответ, у меня фактически нет LayoutParams, но он пока не отображается, поэтому я просто добавил его на всякий случай, это причина, которая пока не отображается. Есть ли LayoutParams вопрос с фрагментом? – jantox

+0

Я просто думаю, что они могут противоречить друг другу. Попробуйте это: измените 'View rootView' на' LinearLayout rootView'. – LuckyMe

ответ

0

Вместо этого hiearchy

[TableLayout < TableRow < TextView ]

попытайтесь добавить непосредственно textview to tablelayout, избегая таблеток.

UPDATE Это отлично работает для меня. Создайте tableLayout, где создайте несколько таблиц с некоторыми текстовыми изображениями внутри.

TableLayout tl; 
    tl = (TableLayout) rootView.findViewById(R.id.fragment1_tlayout); 

    for (int i = 0; i < 30; i++) { 

     TableRow tr = new TableRow(getActivity()); 

     tr.setId(i); 
     tr.setBackgroundResource(R.color.RoyalBlue); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

        //TEXTVIEWS******** 
     TextView tv1 = new TextView(getActivity()); 
     tv1.setText("TEST NUMBER"); 
     tv1.setId(i); 
     tv1.setTextColor(Color.WHITE); 
     tv1.setTextSize(20); 
     tv1.setPadding(5, 5, 5, 5); 
     tr.addView(tv1); 

     TextView tv2 = new TextView(getActivity()); 

     tv2.setText("nº: "+ i); 
     tv2.setId(i+i); 
     tv2.setTextColor(Color.WHITE); 
     tv1.setTextSize(20); 
     tv2.setPadding(5, 5, 5, 5); 
     tr.addView(tv2); 

     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

    } 

XML:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbars="vertical" > 

    <TableLayout 
     android:id="@+id/fragment1_tlayout" 
     android:background="@color/LightGreen" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0,1" > 

    </TableLayout> 
</ScrollView> 
1

У меня такая же проблема, и мое решение надуть расположение строк с точки зрения фрагмента.

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! */ 

}