为什么我的自定义ListView没有显示所有小部件?

时间:2012-10-01 11:51:00

标签: android listview

我正在编写一个带有自定义Listview的约会应用程序。 ListView包含一个

  • 查看,其颜色表示优先级
  • 复选框,用于选择项目。
  • DateText,显示约会的日期
  • MonthText,显示约会的月份
  • TimeText,显示约会时间
  • 标题,存储约会摘要

listView中的每一行应如下所示:

enter image description here

但只有两个小部件可见:DateText和MonthText。其余的不是。为什么会这样?

enter image description here

我的XMLgetView()方法如下:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:id="@+id/priorityView"
        android:layout_width="20dip"
        android:layout_height="wrap_content"
        android:background="#cccccc"
        android:layout_alignParentLeft="true" />

    <CheckBox
        android:id="@+id/selectedCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/priorityView"
        android:layout_alignTop="@id/priorityView"
        android:layout_toRightOf="@id/priorityView"
        android:gravity="center">
    </CheckBox>

    <TextView
        android:id="@+id/dateText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/priorityView"
        android:layout_toRightOf="@id/selectedCheckbox"
        android:gravity="center"
        android:paddingBottom="2dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:paddingTop="2dip"
        android:text="@string/empty"
        android:textSize="20dip"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/monthText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/monthText"
        android:layout_alignLeft="@id/dateText"
        android:layout_alignRight="@id/dateText"
        android:layout_below="@id/dateText"
        android:gravity="center"
        android:padding="5dip"
        android:text="@string/empty"
        android:textSize="10dip" >
    </TextView>

    <TextView
        android:id="@+id/timeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/priorityView"
        android:layout_alignTop="@id/priorityView"
        android:layout_toRightOf="@id/dateText"
        android:gravity="center"
        android:padding="5dip"
        android:text="@string/empty" >
    </TextView>

    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/priorityView"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/priorityView"
        android:layout_toRightOf="@id/timeText"
        android:gravity="center"
        android:padding="5dip"
        android:text="@string/empty" >
    </TextView>

</RelativeLayout>

getView()

public View getView(int position, View convertView, ViewGroup parent) {

                    // List views that go off the screen are recycled.

                    View recycledView = convertView;

                    if (recycledView == null) {
                        /*
                         * If there is no view to recycle (e.g. when the listview is
                         * first created) we must inflate the view from the xml file
                         * using Layout Inflater.
                         */

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

                        recycledView = inflater.inflate(R.layout.row, null);
                    }

                    /*
                     * We now have a view to work with. We will now fill in the contents
                     * of this list view item.
                     */

                    Appointment current = items.get(position);
                    if (current != null) {

                        Log.e("crap",current.toString());

                        View priorityIndicator = (View) recycledView.findViewById(R.id.priorityView);
                        CheckBox selectedCheckbox = (CheckBox) recycledView.findViewById(R.id.selectedCheckbox);
                        TextView dateText = (TextView) recycledView.findViewById(R.id.dateText);
                        TextView monthText = (TextView) recycledView.findViewById(R.id.monthText);
                        TextView timeText = (TextView) recycledView.findViewById(R.id.timeText);
                        TextView titleText = (TextView) recycledView.findViewById(R.id.titleText);

                        //Set priority indictor to appropriate colour.
                        selectedCheckbox.setChecked(true);

                        switch (current.getPriority()) {
                        case PRIORITY_LOW:
                            priorityIndicator.setBackgroundColor(Color.BLUE);
                            break;

                        case PRIORITY_NORMAL:
                            priorityIndicator.setBackgroundColor(Color.GREEN);
                            break;

                        case PRIORITY_HIGH:
                            priorityIndicator.setBackgroundColor(Color.RED);
                            break;

                        default:
                            priorityIndicator.setBackgroundColor(Color.GRAY);
                            break;
                        }

                        //Leave the selected checkbox alone.

                        //Set dateText to show the date of the appointment
                        dateText.setText("29");
                        monthText.setText("SEP");
                        timeText.setText("11:00");
                        titleText.setText(current.getTitle());
                    }

                    return recycledView;
                }

1 个答案:

答案 0 :(得分:1)

尝试使用带有重力属性的线性布局而不是相对布局。相对布局不适合在列表视图中进行膨胀。