我正在编写一个带有自定义Listview的约会应用程序。 ListView包含一个
listView中的每一行应如下所示:
但只有两个小部件可见:DateText和MonthText。其余的不是。为什么会这样?
我的XML
和getView()
方法如下:
<?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;
}
答案 0 :(得分:1)
尝试使用带有重力属性的线性布局而不是相对布局。相对布局不适合在列表视图中进行膨胀。