可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?
Here's how I'm currently populating the spinner. It seems like the simple_spinner_item
is broken on Droid X's.
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry);
回答1:
I'm going to use Spinner
project sample from Android SDK for next code examples.
Code:
First, you need to create you custom adapter which will intercept the creation of views in drop down list:
static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
public CustomArrayAdapter(Context ctx, T [] objects)
{
super(ctx, android.R.layout.simple_spinner_item, objects);
}
//other constructors
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//we know that simple_spinner_item has android.R.id.text1 TextView:
/* if(isDroidX) {*/
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);//choose your color :)
/*}*/
return view;
}
}
Then you create adapter in your code like this:
String [] spin_arry = getResources().getStringArray(R.array.Planets);
this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);
Explanation:
Because CustomArrayAdapter
knows that we use android's built-in layout resource, it also knows that text will be placed in TextView
with id android.R.id.text1
. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.
Screenshot:
回答2:
Simple and Crisp ...
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
回答3:
write a R.layout.simplespinneritem:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The ID is android:id="@android:id/text1"
, set the color of font and background.
ArrayAdapter adapter =
new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);
回答4:
public class ee extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ww);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
Spinner spinner1;
ArrayAdapter<String> mAdapter;
spinner1= (Spinner) findViewById(R.id.spinner2);
spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);
spinner1.setAdapter(mAdapter);
}
}
and in xml res/layout add new xml file: type layout, spinner
(in spinner_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#00f0ff" />
回答5:
To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.
回答6:
make your own layout xml file, and give a android:textColor="#000" for black text
回答7:
Here is more appropriate way guys,
First find the "simple_spinner_item.xml" file in your system,
Follow the below path,
C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout
Now copy the content of "simple_spinner_item.xml" file
Second create the custom_spinner.xml file in your project res\layout folder
and paste the copied content in recently created file
Here is the sample:
res\layout\custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:textAlignment="inherit"
android:ellipsize="marquee"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="@color/dark_gray"
style="?android:attr/spinnerItemStyle"
android:id="@android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android"/>
Here is the set adapter code:
Spinner ddlArea = (Spinner) findViewById(R.id.ddlArea);
ddlArea.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_spinner, areaList));
Where areaList is the List
Thanks,
Ejaz Waquif