我目前在我的应用上有一个自动完成下拉框,但看起来字体颜色是一种灰色,在填充列表时不可见。第二张图显示了点击时下拉列表的外观。如何更改下拉列表的布局以将列表中的项目显示为不同的颜色,如黑色?
下面的照片!
AlertDialog.Builder adb = new AlertDialog.Builder(SettingsMain.this);
LayoutInflater inflater = SettingsMain.this.getLayoutInflater();
final View searchlayout = inflater.inflate(R.layout.search_friend, null);
friend_name = (AutoCompleteTextView) searchlayout.findViewById(R.id.friend_name);
friend_name.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {
}
public void beforeTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
String text = charSequence.toString();
new MyAsyncTask("get").execute("http://10.0.2.2/jamtivia/webservice.php?type=get_friends&username="+text);
drop_down_adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,adapterList);
drop_down_adapter.notifyDataSetChanged();
friend_name.setAdapter(drop_down_adapter);
if (!friend_name.isPopupShowing()) {
friend_name.showDropDown();
}
for(int i =0;i < adapterList.size();i++)
Log.d("TEXT CHNGE", adapterList.get(i));
}
});
adb.setView(searchlayout)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String fname = friend_name.getText().toString();
Integer friend_id = null;
try{
friend_id = adapterListId.get(adapterList.indexOf(fname));
Log.d( "ECHO" , "text : " + name +"ID :"+ player_id+ " ID: " + friend_id);
String request = "http://10.0.2.2/jamtivia/webservice.php?type=add_friends&friendId="+friend_id+"&playerId="+player_id;
new MyAsyncTask("send").execute(request);
}catch(Exception e){Log.d( "PROBLEM SENDING" , "text : " + adapterList.indexOf(fname));}
return;
}
})
.setNegativeButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
adb.show();
另外,就这段代码而言,您可以看到这是执行此功能的好方法吗?
答案 0 :(得分:1)
使用预定义布局创建ArrayAdapter时,请使用自定义布局
像这样<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cust_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@drawable/list_style"
android:textSize="17sp"
android:padding="5dp"
android:gravity="left|center_vertical"/>
//您可以将您选择的颜色或图像作为背景 并根据您的选择命名,让我们说custom_list.xml
创建适配器时使用您的布局
drop_down_adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.custom_list,adapterList);
我希望这会对你有所帮助