如何更改AutoCompleteTextView
的下拉菜单中的文本颜色?或者,如何更改下拉菜单的背景颜色,默认情况下,下拉菜单的背景颜色为白色。如何在AutoCompleteTextView中更改下拉菜单的文字颜色?
回答
默认情况下,实际上不可能直接更改自动完成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")
,因为我想将其修改为.. 现在我认为它会使您变得清晰。
我没有使用任何适配器完成.. –
如果您发现另一种方式来做到这一点,请分享它。 – Cheezmeister
请在这里看到我编辑的答案。请注意这里我有“品牌”作为我的豆(POJO) – NullPointerException
您需要为此创建自定义适配器类,然后您可以将textview设置为这样。
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>
如果您想更改下拉项目的文字颜色,请按照如下操作。
in Adapter class。
@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;
}
让我知道你是否有任何问题。
虽然改变文字颜色可能会很棘手。这是很容易改变液滴的背景颜色选单
AutoCompleteTextView searchView = ...
searchView.setDropDownBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.colorId)));
的
- 1. 更改折叠式下拉菜单中文字的颜色
- 2. 如何在文本更改时显示AutoCompleteTextView的下拉菜单?
- 3. 如何更改在Asp.net中下拉菜单的颜色?
- 4. Bootstrap下拉菜单文字颜色
- 5. 如何更改打开的Bootstrap下拉菜单的颜色?
- 6. 更改SearchView的文本颜色和大小下拉菜单
- 7. 更改引导下拉菜单项的文本颜色
- 8. 在下拉菜单中更改颜色,其中jquery更改乘数下拉菜单
- 9. 在下拉菜单中更改背景颜色 - 移动
- 10. 无法在下拉菜单中获取颜色更改CSS
- 11. 更改mozilla中的selectbox下拉菜单的颜色
- 12. HTML菜单更改仅下拉文本颜色
- 13. 如何在bootstrap中更改下拉内容菜单的背景颜色
- 14. Css下拉菜单字体颜色
- 15. CSS下拉菜单 - 子菜单悬停文字颜色继承
- 16. CSS下拉菜单 - 子菜单颜色
- 17. CSS - 下拉菜单中更改颜色,而徘徊在子菜单项
- 18. 按下后更改菜单的颜色
- 19. 关闭AutoCompleteTextView下拉菜单
- 20. 颜色更改脚本中断slideToggle下拉菜单
- 21. 如何在ABS中更改导航栏下拉菜单fount颜色?
- 22. CSS更改WooCommerce变体的文字颜色下拉菜单WP主题覆盖
- 23. AutoCompleteTextView中的颜色如何在AutoCompleteTextView中显示颜色
- 24. 如何更改Spinner下拉项目的文字颜色?
- 25. 更改颜色和文字的引导下拉单击
- 26. 更改searchview自动完成下拉菜单的背景颜色
- 27. 更改纸张下拉菜单的背景颜色
- 28. 更改悬停下拉菜单的背景颜色
- 29. 更改下拉菜单的背景颜色
- 30. 更改下拉菜单的颜色当点击和悬停时
可能重复的[AutoCompleteTextView背景/前景颜色(http://stackoverflow.com/questions/11787057/autocompletetextview-background-foreground-color) – Sam