问题描述
如何更改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中更改下拉菜单的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!