如何更改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