2011-06-15 79 views
1

我正在尝试创建一个带有两行文本的项目,一行比另一行更大的字体。 TwoLineListItem似乎是我需要的,但是当我尝试运行这样的活动时,应用程序崩溃。有什么建议么?是否可以使用TwoLineListItem填充ListView?

<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" 
    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> 
<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> 

+2

你有任何代码可以告诉我们吗? – 2011-06-15 15:44:08

+0

是的,只是编辑了这个问题 – digipen79 2011-06-15 16:23:26

回答

1

我会夸大你的getView()方法android.R.layout.two_line_list_item和你的价值观绑定到这一点。该布局中的TextView ID是android.R.id.text1android.R.id.text2。下面是一个getView()的例子,它接近你想要的。

 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; 
     }