2012-03-31 68 views
1

我有問題讓我的TableLayout顯示我的行。Android TableLayout不顯示行

我通過這種方法以編程方式傳播的行:

private void buildStatesTable() { 
    Log.d(SelectStatesPanel.class.getCanonicalName(), "Entering Build States Table"); 
    ArrayList<String> states = new ArrayList<String>(); 
    Random rand = new Random(); 

    for(int i = 0; i < 10; i++){ 
     if(i < goodStates.length){ 
      states.add(goodStates[i]); 
     }else{ 
      states.add(badStates[rand.nextInt(badStates.length)]); 
     } 
    } 

    Collections.shuffle(states); 

    TableLayout tl = (TableLayout) findViewById(R.id.statesTable); 
    final int NUM_PER_ROW = 2; 

    for(int i = 0; i < (states.size()/NUM_PER_ROW); i++){ 
     TableRow tr = new TableRow(getContext()); 
     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     for(int j = 0; j < NUM_PER_ROW && (i*NUM_PER_ROW + j) < states.size(); j++){ 
      TextView lblState = new TextView(getContext()); 
      lblState.setText(states.get(i*NUM_PER_ROW + j)); 
      lblState.setTextColor(Color.BLACK); 
      lblState.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      lblState.setClickable(true); 
      lblState.setOnClickListener(this); 
      tr.addView(lblState); 

      //Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding State: " + states.get(i*NUM_PER_ROW + j)); 
      Log.d(SelectStatesPanel.class.getCanonicalName(), "\t\t\t (" + i + ", " + j + ")"); 
     } 

     // Add the TableRow to the TableLayout 
     tl.addView(tr); 
     Log.d(SelectStatesPanel.class.getCanonicalName(), "Adding Row"); 
    } 
} 

這是我的XML(表佈局是在底部):

<?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:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Good choice! There's hope for you yet." 
     android:textSize="16dip" 
     android:textStyle="bold" 
     android:gravity="center" 
     /> 

    <TextView 
     android:id="@+id/txtLex" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Lexington" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtTampa" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Tampa" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtSacramento" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Sacramento" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 
    <TextView 
     android:id="@+id/txtHiltonhead" 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"   
     android:layout_margin="5dip" 

     android:text="Hiltonhead" 
     android:textSize="14dip" 
     android:gravity="center" 
     /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"   
     android:layout_margin="5dip" 

     android:gravity="center" 
     android:src="@drawable/usa" 
     /> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"   
     android:layout_margin="5dip" 
     android:id="@+id/statesTable" 
     android:stretchColumns="0,1">   
    </TableLayout> 


</LinearLayout> 

爲什麼不是我的表格佈局的顯示內容?

回答

0

我自己發現了這個問題。

的問題是這樣的:

 lblState.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

指定TableRow.LayoutParams解決了這一問題。

0

我無法看到具體是怎麼回事錯在你的代碼,但這裏是我自己的代碼類似的執行:

public class EconFragment extends Fragment { 


    private TableLayout questionContainer; 
    static int pos = 0; 
    private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3", 
      "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"}; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     Log.d("Econ", "onCreateView"); 

     return inflater.inflate(R.layout.econfragment, container, false); 
    } 

    public void onResume() { 
     super.onResume(); 


     LayoutInflater inflater = (LayoutInflater) getActivity(). 
     getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     questionContainer = (TableLayout) getActivity().findViewById(R.id.questionContainer); 



     //these lines are my first attempt to try and get the questions to repopulate after returning 
     //from pressing back - as of yet, they don't repopulate the screen: 
     //View econFragment = inflater.inflate(R.layout.econfragment, null); 
     //container.addView(econFragment); 

     int leftMargin=5; 
     int topMargin=5; 
     int rightMargin=5; 
     int bottomMargin=5; 
     while (pos < 10) { 
     View question = inflater.inflate(R.layout.question, null); 
     question.setId(pos); 
     TextView title = (TextView) question.findViewById(R.id.questionTextView); 
     title.setText(titles[pos]); 
     Button charts = (Button) question.findViewById(R.id.chartsButton); 
     charts.setId(pos); 
     charts.setOnClickListener(chartsListener); 
     TableRow tr = (TableRow) question; 
     TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
       TableLayout.LayoutParams.MATCH_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT); 
     trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 
     tr.setLayoutParams(trParams); 

     questionContainer.addView(tr); 
     pos++; 
     } 
     Log.d("Econ", "onResume"); 
    } 

而且questionContainer,econfragment.xml來源:

<?xml version="1.0" encoding="utf-8"?> 

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TableLayout 
      android:id="@+id/questionContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     </TableLayout> 
    </ScrollView> 

如你所見,一個Question.class對象被創建爲EconFragment片段中的一個View對象。問題對象用於爲每個需要的部分查找ViewById。

0

註釋掉整行代碼並將其替換爲兩行文本視圖行。那樣有用嗎?然後,您從中建立行視圖的數組可能是問題。 此致敬禮。

+0

這沒有什麼區別。出現XML中添加的行。所以我不知道發生了什麼事。 – Malfist 2012-03-31 20:09:37