“android.R.layout.simple_list_item_1”是什么?

241

我开始学习Android开发,并跟随一本书的todolist示例:

// Create the array list of to do items
final ArrayList<String> todoItems = new ArrayList<String>();

// Create the array adapter to bind the array to the listView
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(  this, 
                                android.R.layout.simple_list_item_1,
                                todoItems
                            );
myListView.setAdapter(aa);

我无法准确理解这段代码,特别是其中的这一行:

android.R.layout.simple_list_item_1

但是为什么这是一个参数呢?我只想用我的数组更新我的listview,其中一个答案展示了这个。我不确定我为什么需要这个...这是我的问题。谢谢!http://stackoverflow.com/questions/35098789/how-to-update-the-listview-according-to-arraylist/35099000?noredirect=1#comment57929962_35099000 - Ruchir Baronia
7个回答

270

15
布局也包含在您的SDK安装中。 - CommonsWare
11
好的,他们就在那里。:P 我之前试过在Eclipse中浏览Android JAR文件来寻找它们,但只是提示“未找到源代码”。但是没错,它们在platforms>android-x>data>res>layout下面。做得不错。:) - Kevin Coppock
8
这段话的意思是,它告诉ListView在每一行使用哪种布局。还有一些带有CheckedTextView的多选项,您可以创建包含图像和多个TextView的自定义布局等其他类型。而这些android.R中的内容只是为了方便您使用而已,是已经创建好的资源。 - Kevin Coppock
30
谢谢!哇,这是很多布局啊。所有 Android 参考文献似乎只公开了每个 ID 的常量值。可能有任何说明文档可以用一个示例用例_说明_它们吗?(例如,"布局 X 看起来像这样[带有字段 ID 的图形]。在情况 a、b 和 c 中最好使用它。它可以在应用程序 Y 中看到实际效果。") 是的,知道我可以抢劫金库并自己解决这些问题真的很棒,但可扫描的说明列表(与 XML 相对应)将是如此大的帮助! - Joe D'Andrea
13
这似乎是谷歌的典型做法。他们发布所有这些伟大的技术,但文档能力却像IBM一样出色。 - angryITguy
显示剩余7条评论

37

这是安卓操作系统的一部分。这里是已定义XML文件的实际版本。

simple_list_item_1:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/listItemFirstLineStyle"
    android:paddingTop="2dip"
    android:paddingBottom="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

简单列表项2:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/listItemFirstLineStyle"/>

    <TextView android:id="@android:id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
        style="?android:attr/listItemSecondLineStyle" />

</TwoLineListItem> 

13

9

正如Klap所提到的,“android.R.layout.simple_list_item_1是Android操作系统中内置的XML布局文档的引用”
所有布局文件都位于:sdk\platforms\android-xx\data\res\layout
要查看布局的XML:
Eclipse:在代码中键入android.R.layout.simple_list_item_1,按住Ctrl,悬停在simple_list_item_1上,并从下拉菜单中选择“在layout/simple_list_item_1.xml中打开声明”。它将引导您进入XML的内容。
Android Studio:项目窗口 -> 外部库 -> Android X平台 -> res -> layout,在这里您将看到可用布局的列表。
enter image description here


8

android.R.layout.simple_list_item_1是在你的res/layout文件夹中的行布局文件,它包含了listview中对应的设计。现在我们只需要使用mylistview.setadapter(aa)将数组列表项目绑定到行布局即可。


5

无需访问外部链接,您需要的一切都已经在您的计算机上了:

Android\android-sdk\platforms\android-x\data\res\layout.

所有 Android 布局的源代码都在这里。


5

Per Arvand:
Eclipse:只需在代码中键入android.R.layout.simple_list_item_1,按住Ctrl键,在simple_list_item_1上悬停,然后从出现的下拉菜单中选择Open declaration in layout/simple_list_item_1.xml。它将引导您进入XML内容。

从那里开始,如果您将鼠标悬停在编辑器中生成的simple_list_item_1.xml选项卡上,您将看到该文件位于C:\Data\applications\Android\android-sdk\platforms\android-19\data\res\layout\simple_list_item_1.xml(或者是安装的等效位置)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接