Listivew布局在没有@android的情况下无法工作:id / text1

时间:2015-06-15 18:06:49

标签: android listview arraylist android-listview

<?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="match_parent">

<TextView
    android:id="@android:id/text1"
    android:paddingTop="2dip"
    android:paddingBottom="3dip"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="miki"
    android:textSize="30dp"
    android:gravity="center" />


<ImageView
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:id="@+id/ivUserIcon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/key" />

</RelativeLayout>

为什么这段代码不起作用?应用程序崩溃了。

应用程序不会崩溃的唯一方法是:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="miki"
android:textSize="30dp"
android:gravity="center" />

有人可以向我解释为什么会这样吗?我怎样才能添加东西,而不仅仅是TextView

1 个答案:

答案 0 :(得分:1)

您需要创建自定义适配器,因为默认情况下ListView仅支持使用TextView作为根元素的视图(打开XML标记)。

这是一个值得一看的好地方: http://www.vogella.com/tutorials/AndroidListView/article.html#listview_listviewexample

相关问题