Android 更改 Spinner 中的文本颜色
Android change text color in a Spinner
我有一个 Spinner 并使用 ArrayAdapter。在适配器中我使用 "android.R.layout.simple_list_item_1",像这样:
spinnerControlObjectType.setAdapter(new ArrayAdapter(getApplicationContext, android.R.layout.simple_list_item_1, list))
我查看了 android.R.layout.simple_list_item_1 并看到它的文本样式如下:
android:textAppearance="?android:attr/textAppearanceListItemSmall"
我想覆盖我的主题中的 "textAppearanceListItemSmall" 以赋予它不同的颜色,我该怎么做?我不想子类化任何东西或编写代码样板。我确信有一种方法可以仅更改 theme.xml.
来更改颜色
在 android 文档中写道:“...引用样式属性本质上是说,"use the style that is defined by this attribute, in the current theme."...”(http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes)。他们说 "defined" 和 "in the current theme" - 我如何在当前主题中定义它?让我疯狂...
试试这个,
// Initializing an ArrayAdapter
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.spinner_item,plantsList){
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
// Set the Text color
tv.setTextColor(Color.parseColor("#FFC9A3FF"));
return view;
}
};
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(spinnerArrayAdapter);
为您的微调器项目创建自定义 XML 文件
spinner_layout.xml
添加自定义颜色
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_spinner"
android:textSize="16sp"
android:text="HELLO"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
使用此文件显示您的微调项
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_layout,ar);
mSpinner.setAdapter(adapter);
您应该只覆盖主题中的该属性,在本例中我使用 AppCompat 主题作为父主题,但您可以将其更改为任何其他主题。根据您的需要,您应该为不同版本的 Android:
制作主题和样式资源
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="android:textAppearanceListItemSmall">@style/MySpinnerStyle</item>
</style>
<style name="MySpinnerStyle" parent="TextAppearance.AppCompat.Subhead">
<item name="android:textSize">13sp</item>
<item name="android:textColor">@color/white</item>
</style>
我回答我自己的问题 - 一切如文档所述。我的问题是我想在 Spinner 组件中使用 "android.R.layout.simple_list_item_1"。我一使用 "android.R.layout.simple_spinner_item" 它就起作用了。
更改文本颜色。
((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
我有一个 Spinner 并使用 ArrayAdapter。在适配器中我使用 "android.R.layout.simple_list_item_1",像这样:
spinnerControlObjectType.setAdapter(new ArrayAdapter(getApplicationContext, android.R.layout.simple_list_item_1, list))
我查看了 android.R.layout.simple_list_item_1 并看到它的文本样式如下:
android:textAppearance="?android:attr/textAppearanceListItemSmall"
我想覆盖我的主题中的 "textAppearanceListItemSmall" 以赋予它不同的颜色,我该怎么做?我不想子类化任何东西或编写代码样板。我确信有一种方法可以仅更改 theme.xml.
来更改颜色在 android 文档中写道:“...引用样式属性本质上是说,"use the style that is defined by this attribute, in the current theme."...”(http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes)。他们说 "defined" 和 "in the current theme" - 我如何在当前主题中定义它?让我疯狂...
试试这个,
// Initializing an ArrayAdapter
final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.spinner_item,plantsList){
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
// Set the Text color
tv.setTextColor(Color.parseColor("#FFC9A3FF"));
return view;
}
};
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(spinnerArrayAdapter);
为您的微调器项目创建自定义 XML 文件
spinner_layout.xml
添加自定义颜色
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_spinner"
android:textSize="16sp"
android:text="HELLO"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
使用此文件显示您的微调项
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_layout,ar);
mSpinner.setAdapter(adapter);
您应该只覆盖主题中的该属性,在本例中我使用 AppCompat 主题作为父主题,但您可以将其更改为任何其他主题。根据您的需要,您应该为不同版本的 Android:
制作主题和样式资源<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="android:textAppearanceListItemSmall">@style/MySpinnerStyle</item>
</style>
<style name="MySpinnerStyle" parent="TextAppearance.AppCompat.Subhead">
<item name="android:textSize">13sp</item>
<item name="android:textColor">@color/white</item>
</style>
我回答我自己的问题 - 一切如文档所述。我的问题是我想在 Spinner 组件中使用 "android.R.layout.simple_list_item_1"。我一使用 "android.R.layout.simple_spinner_item" 它就起作用了。
更改文本颜色。
((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);