无法正确显示在运行时添加的视图

时间:2012-03-19 22:01:58

标签: android user-interface layout-inflater android-inflate

这是我的主要布局:

<RelativeLayout style="@style/StandardFillWrap" >

    <TableLayout
        android:id="@+id/match_tab"
        style="@style/StandardFillWrap"
        android:stretchColumns="*" >

        <TableRow >

            <TextView android:id="@+id/match_home_name" />

            <TextView android:id="@+id/match_home_score" />
        </TableRow>

        <TableRow >

            <TextView android:id="@+id/match_away_name" />

            <TextView android:id="@+id/match_away_score" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/match_filter_b"
        style="@style/StandardFillWrap"
        android:layout_below="@id/match_tab"
        android:text="@string/l_matchView_filters" />

    <LinearLayout android:id="@+id/match_list"
        style="@style/StandardWrapWrap" 
        android:layout_below="@id/match_filter_b"
        />
    <TableLayout
        android:id="@+id/match_reparti"
        style="@style/StandardFillWrap"
        android:layout_below="@id/match_list"
        android:stretchColumns="*" >

        <TableRow >

            <TextView
                android:id="@+id/match_home_name_det"
                style="@style/NomePlayer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_indirect_set_pieces" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_defence" />

            <TextView android:id="@+id/match_home_set_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_attack" />

            <TextView android:id="@+id/match_home_set_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/vuoto" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_team_attitude" />

            <TextView android:id="@+id/match_home_team_attitude" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_tactic" />

            <TextView android:id="@+id/match_home_team_tactic" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_tactic_skill" />

            <TextView android:id="@+id/match_home_team_tactic_skill" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/vuoto" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_midfield" />

            <TextView android:id="@+id/match_home_team_midfield" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_right_defence" />

            <TextView android:id="@+id/match_home_team_r_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_central_defence" />

            <TextView android:id="@+id/match_home_team_c_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_left_defence" />

            <TextView android:id="@+id/match_home_team_l_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_right_attack" />

            <TextView android:id="@+id/match_home_team_r_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_central_attack" />

            <TextView android:id="@+id/match_home_team_c_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_left_attack" />

            <TextView android:id="@+id/match_home_team_l_att" />
        </TableRow>

        <TableRow >

            <TextView
                android:id="@+id/match_away_name_det"
                style="@style/NomePlayer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_indirect_set_pieces" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_defence" />

            <TextView android:id="@+id/match_away_set_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_attack" />

            <TextView android:id="@+id/match_away_set_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/vuoto" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_team_attitude" />

            <TextView android:id="@+id/match_away_team_attitude" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_tactic" />

            <TextView android:id="@+id/match_away_team_tactic" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_tactic_skill" />

            <TextView android:id="@+id/match_away_team_tactic_skill" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/vuoto" />

            <TextView android:text="@string/vuoto" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_midfield" />

            <TextView android:id="@+id/match_away_team_midfield" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_right_defence" />

            <TextView android:id="@+id/match_away_team_r_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_central_defence" />

            <TextView android:id="@+id/match_away_team_c_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_left_defence" />

            <TextView android:id="@+id/match_away_team_l_def" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_right_attack" />

            <TextView android:id="@+id/match_away_team_r_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_central_attack" />

            <TextView android:id="@+id/match_away_team_c_att" />
        </TableRow>

        <TableRow >

            <TextView android:text="@string/l_matchView_left_attack" />

            <TextView android:id="@+id/match_away_team_l_att" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

填写match_list的方法是:

private void mimicListAdapter(List<Event> eventsList, String filters) {

    TextView minute;
    ImageView event_key;
    TextView event_text;
    LayoutInflater inflater = getLayoutInflater();
    LinearLayout container = (LinearLayout) findViewById(R.id.match_list); 

    for(Event p:eventsList) {
        //LayoutInflater inflater = getLayoutInflater();
        View rowView = inflater.inflate( R.layout.match_event, null, true);
        minute = (TextView) rowView.findViewById(R.id.minute);
        event_key = (ImageView) rowView.findViewById(R.id.event_img);
        event_text = (TextView) rowView.findViewById(R.id.event_text);
        boolean found = false;
        String[] events = getResources().getStringArray(R.array.filtering);
        if(filters!=null)
        {
            if(filters!="")
            {
                char[] indici = filters.toCharArray();
                for(int i = 0; i<indici.length; i++){
                    if(events[Character.getNumericValue( indici[i] )].contains(","+p.getEventKey()+","))
                    {
                        found=true;
                        break;
                    }
                }   
            }

        }
        minute.setText(Integer.toString(p.getMinute()));
        event_text.setText(p.getEventText());
        try {
            Drawable img = getResources().getDrawable( getResources().getIdentifier("i_"+p.getEventKey(), "drawable", getPackageName()));
            event_key.setImageDrawable(img);
        } catch (Exception e) {

        }

        if(found) {
            event_text.setTextColor(0xFFFF0000);
            minute.setTextColor(0xFFFF0000);
            found = false;
        }   

        container.addView(rowView);
    }
}

我遇到的问题是,match_list以外的任何内容都正确显示,在match_list中我可以看到第一个元素但是它全部为空白。我的意思是,由于占据了很多显示器,因此LinearLayout似乎已被填满,但它只是黑色空间,只有第一个Event可见。

2 个答案:

答案 0 :(得分:1)

将视图添加到容器时,您应该传递有关如何使用LayoutParams类显示它的信息。现在,您缺少使用layout_widht和layout_height在布局xml中另外定义的信息。如果没有它们,我猜Android会设置第一个视图,但由于缺少大小信息而无法对齐其他视图。像这样:

container.addView(rowView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

我真是个白痴。我错过了LinearLayout的垂直方向。

BTW我发现听了这个关于ListView的Google I / O. http://www.youtube.com/watch?v=wDBM6wVEO70