什么是"android.R.layout.simple_list_item_1"?

Zak*_*ria 228 layout android android-arrayadapter

我已经开始学习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);
Run Code Online (Sandbox Code Playgroud)

我无法理解这段代码特别是这一行:

android.R.layout.simple_list_item_1
Run Code Online (Sandbox Code Playgroud)

kco*_*ock 260

Zakaria,它是对作为Android OS一部分的内置XML布局文档的引用,而不是您自己的XML布局之一.

以下是您可以使用的更多布局列表:http: //developer.android.com/reference/android/R.layout.html
(更新链接感谢@Estel:https://github.com/android/platform_frameworks_base/ tree/master/core/res/res/layout)

您实际上可以查看布局的代码.

  • 谢谢!哇,这是很多布局.所有Android Reference似乎都揭示了它们(在R.layout.html中)是每个id的常量值.可能有任何文档_illustrates_每个与示例用例?(例如,"布局X看起来像这样[带有字段ID的图].最好用于案例a,b和c.它可以在应用程序Y中看到.")是的,很高兴知道我可以掠夺金库并自行破解这一切,但可扫描的插图列表(与XML相比)将是一个很大的帮助! (29认同)
  • 布局也在SDK安装中 (15认同)
  • 这似乎是Google的典型做法.他们发布了所有这些伟大的技术,并拥有IBM的所有文档技能. (11认同)
  • 嘿,他们是.:P我之前只是通过在Eclipse中浏览android jar而尝试寻找它们,它只是告诉我"Source Not Found".但是,是的,他们是在平台> android-x>数据> res>布局.好决定.:) (10认同)
  • 它告诉列表视图用于各行的布局.还有其他一些用于多个选择的checkedtextviews,您可以制作包含图像的自定义布局,例如多个textview.这些android.R只是一些易于使用,已经为您创建的资源. (8认同)
  • 我什至无法计算仅仅因为找不到`simple_list_item_1`而停止关注的教程数量。 (2认同)

nik*_*o28 35

这是Android操作系统的一部分.这是定义的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" />
Run Code Online (Sandbox Code Playgroud)

simple_list_item_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> 
Run Code Online (Sandbox Code Playgroud)


Nig*_*sto 13

如上所述:kcoppock和Joril

去这里:https://github.com/android/platform_frameworks_base/tree/master/core/res/res/layout

只需右键单击所需的布局文件,然后选择"另存为",保存到某处,然后将其复制到Android项目中的"布局"文件夹中(eclipse)...

你可以看看布局是怎么样的:)

要走的路......


Arv*_*and 9

如Klap所述"android.R.layout.simple_list_item_1是对作为Android OS一部分的内置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 - >布局,在这里您将看到可用布局列表.
在此输入图像描述


小智 7

android.R.layout.simple_list_item_1,这是res/layout文件夹中的行布局文件,其中包含行的相应设计listview.现在我们通过使用将数组列表项绑定到行布局mylistview.setadapter(aa);


Aar*_*lap 5

无需转到外部链接,您需要的所有内容都已存在于您的计算机上:

Android的\ Android的SDK \平台\ Android的-X\DATA \水库\布局.

所有Android布局的源代码都位于此处.


Bar*_*oyd 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(或安装的等效位置)。