如何更改 AutoCompleteTextView 中下拉菜单的文本颜色? [英] How to change the text color of dropdown in an AutoCompleteTextView?

查看:30
本文介绍了如何更改 AutoCompleteTextView 中下拉菜单的文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改 AutoCompleteTextView 下拉菜单中的文本颜色?或者如何更改下拉菜单的背景颜色,默认情况下下拉菜单的背景颜色为白色.

How can I change the text color in dropdown menu of an AutoCompleteTextView? Or how can I change the background color of a dropdown, by default dropdown's background color is white.

推荐答案

您需要为此创建自定义适配器类,然后您可以将 textview 设置为这样.

you need to create custom adapter class for this, then you can set textview to this like.

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

这里是 autocompletelistview_test1

Here is 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" />

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

use following code to change background color of dropdown like

 autocomplete.setDropDownBackgroundResource(R.color.autocompletet_background_color);  

你可以在string.xml中设置这样的值

and you can set value like this in string.xml

   <color name="autocompletet_background_color">#EFEFEF</color>    

如果您想更改下拉项的文本颜色,请执行以下操作.

If you want to change text color of dropdown item then do like following.

在适配器类中.

 @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;
}    

如果您有任何问题,请告诉我.

Let me know if you have any question.

这篇关于如何更改 AutoCompleteTextView 中下拉菜单的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆