微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以使用TwoLineListItem填充ListView?

如何解决是否可以使用TwoLineListItem填充ListView?

| 我正在尝试使用具有两行文本的项目创建一个ListView,其中一行具有比另一行更大的字体。 TwoLineListItem似乎是我所需要的,但是每当我尝试像这样运行活动时,应用程序就会崩溃。有什么建议么?
<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>
    

解决方法

        我会在您的
getView()
方法中将
android.R.layout.two_line_list_item
膨胀,并将您的值绑定到该方法。该布局中的TextView id是
android.R.id.text1
android.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;
        }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。