什么是android.R.layout.simple_list_item_1?

时间:2011-05-21 03:03:29

标签: java android listview android-arrayadapter

在所有示例中,我看到他们在创建ArrayAdapter时只使用“android.R.layout.simple_list_item_1”。 什么是android.R.layout.simple_list_item_1,它只是名为simple_list_item_1.xml的布局文件的名称还是数组适配器所需的TextView的id?

如何查看文件内容或使用我自己的res文件夹中的文件?

public class MyClass extends ListActivity {
private String[] titles = {"Test"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, titles));
     updateList();
}
}

3 个答案:

答案 0 :(得分:29)

android.R.layout包含Android操作系统用于显示各种项目的所有公开布局。 android.R.layout.simple_list_item_1就像它的名字一样,只是一个简单的布局来显示一段文字。它使您无需在使用适配器时编写简单的布局,还可以轻松地为应用程序提供系统的本机外观和主题。

我已添加from the GitHub mirror repo

的来源android.git.kernel.org
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>

答案 1 :(得分:2)

Android API中有一些内置布局XML文件 并且在此图像中列出

enter image description here

android.R.layout.simple_list_item_1就是其中之一 它用于简单显示String

您可以使用自己的布局而不是android.R.layout.simple_list_item_1

例如,如果您已创建布局row.xml,则可以使用

setListAdapter(new ArrayAdapter<String>(this, R.layout.row, titles));

答案 2 :(得分:0)

android.R.layout.simple_list_item_1是一个内置的布局资源,它显示一个字符串。 如果您想使用自己的布局文件,则可以使用

setListAdapter(new ArrayAdapter<String>(this, R.layout.<your layout filename>, titles));