简体   繁体   English

ListView中的布局文件在哪里?

[英]Where is the layout file in my ListView?

I am new to android and i am confused about something. 我是Android新手,对某些事情感到困惑。 I have created a simple ListView, here is my code: 我创建了一个简单的ListView,这是我的代码:

public class MainActivity extends ListActivity {

    ArrayList<String> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listItems = new ArrayList<String>();
        for(int i=0;i<20;i++){
            listItems.add("List Item #"+i);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);
        setListAdapter(adapter);
    }
}

This correctly displays the list items on the screen, but here is my question: I have no xml files, no layouts in my project and i do not use setContentView function here. 这样可以在屏幕上正确显示列表项,但这是我的问题:我的项目中没有xml文件,也没有布局,在这里我不使用setContentView函数。 So why is this code working? 那么为什么此代码有效? How can it be displayed even though there is no xml files or layouts? 即使没有xml文件或布局,也如何显示?

Thanks 谢谢

You are using android.R.layout.simple_list_item_1 as the layout, which provides a TextView for you to work with. 您正在使用android.R.layout.simple_list_item_1作为布局,该布局提供了TextView供您使用。 This is a layout provided by Android for simple lists of text. 这是Android为简单文本列表提供的布局。

If you want to make a more complex layout, you will/can define your own layout. 如果要制作更复杂的布局,则可以/可以定义自己的布局。

Android ships with default containers for list items. Android随附了用于列表项的默认容器。

As the name suggests, android.R.layout.simple_list_item_1 belongs to the android.R package and not to your app's resources. 顾名思义, android.R.layout.simple_list_item_1属于android.R包,而不属于您应用的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM