Is it possible to populate a ListView with a TwoLineListItem?
I'm trying to create a ListView with items that have two lines of text, one line with a larger font than the other. TwoLineListItem seems to be what I need, but the app crashes whenever I try to run the activity like that. Any suggestions?
<TableLayout
android:id="@+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>
<TextView android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView android:id="@android:id/text2"
android:layout_width="fill_parent"
androi开发者_JAVA百科d:layout_marginLeft="5dip"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</TwoLineListItem>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>
<TextView android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView android:id="@android:id/text2"
android:layout_width="fill_parent"
android:layout_marginLeft="5dip"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</TwoLineListItem>
I would inflate android.R.layout.two_line_list_item
in your getView()
method and bind your values to that. The TextView ids in that layout are android.R.id.text1
and android.R.id.text2
. Below is an example of a getView() that gets close to what you want.
public View getView(int pos, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(MyActivity.this, android.R.layout.two_line_list_item, null);
}
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("Big Font Text");
text = (TextView) view.findViewById(android.R.id.text2);
text.setText("Small Font Text");
return view;
}
精彩评论