I have been unable to set the color on the spinner widget. How is this styled?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Try using this adapter for your spinner:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
{
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.white)
);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_bg);
((TextView) v).setTextColor(
getResources().getColorStateList(R.color.spinner_text)
);
((TextView) v).setTypeface(fontStyle);
((TextView) v).setGravity(Gravity.CENTER);
return v;
}
};
Add this xml to your layout,
my_spinner_style.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
And at last,
spinner.setAdapter(adapter);
回答2:
Simple and crisp ....
private OnItemSelectedListener your_spinner _name= new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
回答3:
The simplest form is:
m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
回答4:
If you want to change the text color : Spinner Widgets Text Color
Else, making your own layout is the best way like JoxTraex said.
回答5:
A shorter alternative to Andro'd answer is to let the ArrayAdapter
create the item views for you from a layout resource:
final List<String> values = [SPINNER VALUES];
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
activity, R.layout.my_spinner_item, values);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
spinner.setAdapter(adapter);
Then style your text to suit your needs in my_spinner_item.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/my_spinner_item_style"
/>
Note: my_spinner_dropdown_item
is used when the list of choices appear
For more information read the Spinners documentation.
回答6:
Try understanding that you are using the dropdown list as provided by the defaults that are available in the SDK.
SIMPLY make your own layout with a custom adapter.