如何在AutoCompleteTextView中更改下拉菜单的文本颜色?

6
我该如何更改AutoCompleteTextView下拉菜单中的文本颜色?或者,我该如何更改下拉菜单的背景颜色,因为默认下拉菜单的背景颜色是白色。

可能是AutoCompleteTextView背景/前景颜色的重复问题。 - Sam
4个回答

12

你需要为此创建自定义适配器类,然后可以将文本视图设置为如下所示。

autoAdapter = new AutoAdapter(this, R.layout.autocompletelistview_test1,
            edittext);    

这是autocompletelistview_test1。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="44dip"
android:gravity="center_vertical"

android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="12dip"
android:textStyle="bold"
android:textColor="#cc3333"
android:id="@+id/textview"
android:textSize="14sp" />

使用以下代码更改下拉菜单的背景颜色:

 autocomplete.setDropDownBackgroundResource(R.color.autocompletet_background_color);  

你可以像这样在string.xml文件中设置值

   <color name="autocompletet_background_color">#EFEFEF</color>    
如果您想更改下拉项的文本颜色,则可以按照以下方式操作。
在适配器类中进行操作。
 @Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }
    TextView text = (TextView) mView.findViewById(R.id.textview);        
    if(getItem(position) != null )
    {
        text.setTypeface(faceBold);
        if(getItem(position).equals("check some condition if you want. "))
        {
            text.setTextColor(Color.parseColor("#999999"));
            text.setText("set some text");               
        }           
        else
        {
            text.setTextColor(Color.parseColor("#cc3333"));
                text.setText(getItem(position));
        }           
    }
    return mView;
}    

如果你有任何问题,请告诉我。


4

虽然改变文本颜色可能有点棘手,但更改下拉菜单的背景颜色很容易。

AutoCompleteTextView searchView = ... 
searchView.setDropDownBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.colorId)));

1

我使用自定义适配器解决了这个问题,而不需要膨胀布局。

  1. Simply create a layout autocomplete_custom.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/any_id"
    android:textColor="#ffffff"/>
    
  2. Use it in your adapter

    autocomplete.setAdapter(new ArrayAdapter<String>(this,
            R.layout.autocomplete_custom, listContainingText););
    

运行。Bingo!


0

实际上,默认情况下无法直接更改自动完成textView的颜色。相反,您需要为此实现自定义适配器,并通常从适配器中填充视图以使用textView。 参考this这个问题,它将帮助您解决问题。因此,您可以根据需要设置textview的属性。 例如,您必须有以下自定义适配器

public class MyCustomBrandAdapter extends ArrayAdapter<Brand>{


List<Brand> Brandlist;
Context context;
private ArrayList<Brand> itemsAll;
private ArrayList<Brand> suggestions;
public MyCustomBrandAdapter(Context context, int textViewResourceId, List<Brand> Brandlist) 
{
    
    super(context,  textViewResourceId, Brandlist);
    this.Brandlist =  Brandlist;
    this.itemsAll = (ArrayList<Brand>) ((ArrayList<Brand>) Brandlist).clone();
    this.suggestions = new ArrayList<Brand>();
    this.context = context;
}


public View getView(int position, View convertView, ViewGroup parent) 
{
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.listview, null);

    }
    Brand Brand = Brandlist.get(position);
    if (Brand != null) {
        TextView tt = (TextView) v.findViewById(R.id.txtName);
        
        if (tt != null) {
            tt.setText(Brandlist.get(position).toString());
        } else {
            System.out.println("Not getting textview..");
        }
    
    }
    return v;
}
 @Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        public String convertResultToString(Object resultValue) {
            String str = ((Brand)(resultValue)).getBrandDescription1(); 
            return str;
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (Brand brand : itemsAll) {
                    if(brand.getBrandDescription1().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        suggestions.add(brand);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<Brand> filteredList = (ArrayList<Brand>) results.values;
            List<Brand> getResult=new ArrayList<Brand>();
            if(results != null && results.count > 0) {
                clear();
                for (Brand c : filteredList) {
                    getResult.add(c);
                }
                Iterator<Brand> brandIterator=getResult.iterator();
                while(brandIterator.hasNext()){
                    Brand party=brandIterator.next();
                    add(party);
                }
                notifyDataSetChanged();
            }
        }
    };
    
}

而在这里,布局R.layout.listview的XML如下所示

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#000000" 
            android:textSize="12dp"/>

    </LinearLayout>

</LinearLayout>

在这个xml文件中,我将背景设置为白色(android:background="#ffffff"),我想要文本颜色为黑色(android:textColor="#000000"),大小为(android:textSize="12dp"),因为我想要修改它。我认为现在你应该明白了。

2
如果你找到了另一种方法来实现它,请分享一下。 - Cheezmeister
请查看我的编辑后的答案。请注意,这里我将“Brand”作为我的bean(POJO)。 - NullPointerException

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接