11

我制作了一个 AutoCompletetextView。AutoCompleteTextView 下拉列表中的项目不可见。如何更改这些项目的颜色。

这是它的外观:

在此处输入图像描述

4

7 回答 7

14

为了控制您在自动完成视图中显示项目的方式,您必须在适配器中设置 textViewResourceId。您可以使用 ArrayAdapter 并android.R.layout.simple_dropdown_item_1line作为 textViewResourceId 提供,如下所示。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, yourList);
AutoCompleteTextView autocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_box);
autocompleteView.setAdapter(adapter);

或者

如果您想为显示的项目创建自己的样式,请创建一个TextView像这样作为根元素的 XML(让我们my_custom_dropdown.xml用黑色文本和白色背景命名)

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:padding="5sp"
    android:textColor="@color/black"
    android:background="@color/white"/>

然后参考适配器中的 xml,如下所示 -

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_dropdown, yourList);
于 2012-07-11T10:03:20.210 回答
7

只是要指出,使用android.R.layout.simple_dropdown_item_1line它会给你上面遇到的同样的问题。所以你最好TextView在一个.xml文件中创建你自己的。

于 2012-07-23T13:43:03.110 回答
5

如果将代码从 更改"android.R.layout.simple_list_item_1"为 对您"android.R.layout.simple_dropdown_item_1line" 不起作用

您应该尝试在setContentView之前编写此代码

setTheme(android.R.style.Theme);

它对我有用:)

于 2013-08-12T10:03:14.360 回答
0

只需使用"android.R.layout.simple_list_item_1"而不是"android.R.layout.simple_dropdown_item_1line".....您的问题将得到解决... :)

于 2012-12-06T07:14:01.337 回答
0

我创建了一个示例项目:AutoCompleteTextViewAdapter(Github Repo)

您需要实现以下源代码行:

活动

ArrayAdapter<String> myCustomAdapter = new ArrayAdapter<String>(this, R.layout.text_custom_view, countriesNames);

final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view);
textView.setAdapter(myCustomAdapter);

带有自定义 TextView 的 XML

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/country_name"
    style="@style/CustomTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true" />

最后结果

自动完成文本视图适配器

于 2015-05-19T12:05:48.840 回答
0

这是答案,希望其他人会受益

http://www.outofwhatbox.com/blog/2010/11/android-simpler-autocompletetextview-with-simplecursoradapter/

public class SelectState extends Activity {

final static int[] to = new int[] { android.R.id.text1 };  

    final static String[] from = new String[] { "state" };

    private TextView mStateCapitalView;
    private AutoCompleteTextView mStateNameView;
    private AutoCompleteDbAdapter mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new AutoCompleteDbAdapter(this);
        setContentView(R.layout.selectstate);
        Button confirmButton = (Button) findViewById(R.id.confirm);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });

        mStateCapitalView = (TextView) findViewById(R.id.state_capital);
        mStateNameView = (AutoCompleteTextView) findViewById(R.id.state_name);

        // Create a SimpleCursorAdapter for the State Name field.
        SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, 
                    android.R.layout.simple_dropdown_item_1line, null,
                    from, to);
        mStateNameView.setAdapter(adapter);

        // Set an OnItemClickListener, to update dependent fields when
        // a choice is made in the AutoCompleteTextView.
        mStateNameView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view,
                        int position, long id) {
                // Get the cursor, positioned to the corresponding row in the
                // result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String capital = 
                    cursor.getString(cursor.getColumnIndexOrThrow("capital"));

                // Update the parent class's TextView
                mStateCapitalView.setText(capital);
            }
        });

        // Set the CursorToStringConverter, to provide the labels for the
        // choices to be displayed in the AutoCompleteTextView.
        adapter.setCursorToStringConverter(new CursorToStringConverter() {
            public String convertToString(android.database.Cursor cursor) {
                // Get the label for this row out of the "state" column
                final int columnIndex = cursor.getColumnIndexOrThrow("state");
                final String str = cursor.getString(columnIndex);
                return str;
            }
        });

        // Set the FilterQueryProvider, to run queries for choices
        // that match the specified input.
        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                // Search for states whose names begin with the specified letters.
                Cursor cursor = mDbHelper.getMatchingStates(
                        (constraint != null ? constraint.toString() : null));
                return cursor;
            }
        });
    }
}

请检查 line final static int[] to = new int[] { android.R.id.text1 };

于 2015-07-06T15:16:42.503 回答
0

这是最简单的方法。创建您自己的 TextView xml 文件。例如 custom_textview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/tv"
     android:textColor="@color/font_content"
     android:padding="5sp"
     android:textSize="16sp"
     android:layout_width="match_parent"
     android:background="@drawable/any_background"
     android:singleLine="true"
     android:gravity="center"
     android:layout_height="match_parent"/>

然后在你的活动中

    //Create adapter object
    ArrayAdapter<String> adpt = new ArrayAdapter<String>(context,R.layout.mytextview, what_ever_array_object_here);
    searchAutoComplete.setAdapter(adpt);
于 2018-02-26T23:15:56.723 回答