Android TableLayout不显示行

时间:2012-03-31 02:39:16

标签: android android-layout

我在让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>

为什么我的桌面布局没有显示内容?

3 个答案:

答案 0 :(得分: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>

如您所见,在EconFragment片段中创建一个Question.class对象作为View对象。问题对象用于查找每个所需部分的ViewById。

答案 1 :(得分:0)

注释掉整行代码并将其替换为双线文本视图行。那样有用吗?那么构建行视图的数组可能就是问题所在。 最好的问候。

答案 2 :(得分:0)

我自己发现了这个问题。

问题在于:

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

指定TableRow.LayoutParams解决了问题。