我不明白 Android ArrayAdapter 的 AutoCompleteTextView 构造函数

I don't understand Android AutoCompleteTextView Constructor of ArrayAdapter

我想在 android 中使用 AutoCompleteTextView 并阅读有关它的官方 developer.android 文档。

有一个代码片段如下所示:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, COUNTRIES);
     AutoCompleteTextView textView = (AutoCompleteTextView)
             findViewById(R.id.countries_list);
     textView.setAdapter(adapter);
 }

 private static final String[] COUNTRIES = new String[] {
     "Belgium", "France", "Italy", "Germany", "Spain"
 };

不明白ArrayAdapter的构造函数中的第二个参数(android.R.layout.simple_dropdown_item_1line)是什么意思,这是从哪里来的?

它是可从 android 获得的布局还是我必须用我自己创建的布局替换此布局以及在这种情况下如何定义此布局文件?

具体我的代码 lokks like xml:

<AutoCompleteTextView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

java:

AutoCompleteTextView search =(AutoCompleteTextView) findViewById(R.id.search);
String[] vocabs = new String[1001];
//fill the String array
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line ,vocabs);
search.setAdapter(adapter);

他们正在使用 3 个参数 (documentation) 调用 ArrayAdapter 的构造函数:ArrayAdapter(Context context, int resource, T[] objects)

资源 R.layout.simple_dropdown_item_1line 只是下拉菜单的默认 android 框架布局之一。请参阅 here 其他默认布局列表。

编辑以回答您的第二个问题

您可以使用默认的 android 布局(您提供的示例应该有效)或您定义的自定义布局。如果是最后一种情况,那么只需为此布局创建一个 xml 布局:

layouts/dropdown_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/vocab_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="vocab"/>
</LinearLayout>

然后您可以使用 ArrayAdapter 构造函数 ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) 指向您的自定义布局和您想要填充词汇的 TextView:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_custom_view, R.id.vocab_text ,vocabs);
search.setAdapter(adapter);

还要检查您是否正确填充了 vocabs 数组。

此布局在 Android 系统中可用,这就是您使用 android.R 的原因。它用于显示数组中的项目 adapter.It 基本上是具有某些样式的文本视图

您可以使用自定义布局 AutoCompleteTextView

ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text_title, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e4e4e4"
    >

    <TextView
        android:id="@+id/text_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        tools:text="AA"
        android:padding="15dp"
        />
</LinearLayout>