In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it's working properly. Here is the code for that.
在我的Android应用程序中,我使用了spinner,并且我已经将SQLite数据库中的数据加载到spinner中,并且工作正常。这是它的代码。
Spinner spinner = (Spinner) this.findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, list);
cursor.moveToFirst();
list.add("All Lists");
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
keyList[i] = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.KEYWORD));
list.add(keyList[i]);
cursor.moveToNext();
}
}
Database.close();
cursor.close();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Now I want to change the text color and text size of spinner data. I have used following XML lines to my spinner tag on my XML file, but it is not working.
现在我要更改微调数据的文本颜色和文本大小。我在XML文件上使用了如下的XML行作为spinner标记,但它不起作用。
android:textColor="@android:color/white"
android:textSize="11dp"
How can I change the text color and text size of my spinner?
如何更改微调器的文本颜色和文本大小?
19 个解决方案
#1
611
Make a custom XML file for your spinner item.
为微调项创建自定义XML文件。
spinner_item.xml:
spinner_item.xml:
Give your customized color and size to text in this file.
将您的自定义颜色和大小发送到该文件中的文本。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
现在使用这个文件来显示你的微调项,比如:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set the drop down resource. It will take spinner_item.xml
only to show your items in spinner.
您不需要设置下拉资源。它需要spinner_item。xml只显示您的项目在旋转。
#2
153
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
119
If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:
如果所有的旋转器的TextView项都有相同的文本颜色,另一种方法是为spinner下拉项使用自定义样式:
In res/values/styles.xml
:
在res /价值/ styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/my_spinner_text_color</item>
</style>
</resources>
And define your custom color in res/values/colors.xml:
并在res/values/colors.xml中定义自定义颜色。
<color name="my_spinner_text_color">#808080</color>
#4
56
Here is a link that can help you to change the color of the Spinner:
这里有一个链接可以帮助你改变旋转器的颜色:
点击这里
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>
You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:
您需要为spinner项目spinner_item.xml创建具有自定义定义定义的布局文件:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:
如果您想要定制下拉列表项,您需要创建一个新的布局文件。spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
And finally another change in the declaration of the spinner:
最后,旋转器声明的另一个变化是
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
That's it.
就是这样。
#5
14
To prevent lagging, you need to not only set the text properties in the onItemSelected
listener, but also in the Activity's onCreate
method (but it's a little tricky).
为了防止滞后,您不仅需要在onItemSelected侦听器中设置文本属性,还需要在活动的onCreate方法中设置文本属性(但这有点棘手)。
Specifically, you need to put this in onCreate
after setting the adapter:
具体来说,您需要在设置适配器后将其放入onCreate:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected
:
然后把这个放到onItemSelected:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
这里有一个完整的例子:
@Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.
有关更多细节,请参见我的问题。
#6
10
If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.
如果您希望文本颜色只在选定的项目中更改,那么这可能是一个解决方案。它对我有用,也应该对你有用。
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});
#7
5
Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:
与其定制一个小尺寸的布局,如果你想使用Android的内部小尺寸的布局,你应该使用:
"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".
“android.R.layout。simple_gallery_item”而不是“android.R.layout.simple_spinner_item”。
ArrayAdapter<CharSequence> madaptor = ArrayAdapter
.createFromResource(rootView.getContext(),
R.array.String_visitor,
android.R.layout.simple_gallery_item);
It can reduce the size of spinner's layout. It's just a simple trick.
它可以减少纺纱机布局的大小。这只是一个简单的技巧。
If you want to reduce the size of a drop down list use this:
如果你想减少下拉列表的大小,请使用以下方法:
madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
#8
5
If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:
如果你使用android.support.v7.widget。AppCompatSpinner是使用样式测试的最简单的解决方案:
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spefcialFx"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="@style/Spinner"
android:entries="@array/special_fx_arrays"
android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
And the style:
和风格:
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:backgroundTint">@color/red</item>
<item name="android:textSize">14sp</item>
</style>
The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.
唯一的缺点是android:backgroundTint为下拉箭头和下拉背景设置颜色。
#9
3
For someone who needs only Style
way for AppCompat
.
对于那些只需要使用AppCompat的方式的人。
结果
styles.xml
styles.xml
<resources>
...
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/material_grey_700</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
your_spinner_layout.xml
your_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/content_spinner"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:entries="@array/shipping_tracking_carrier_names"
android:spinnerMode="dropdown"
android:theme="@style/Spinner" />
<EditText
android:id="@+id/content_input"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="@color/material_grey_700"
android:textSize="12sp" />
...
</LinearLayout>
Plus
And if you want to set android:entries
programmatically with defined style.
Try this.
另外,如果您想要设置android:以编程方式定义样式的条目。试试这个。
AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
As in the code, using same Context
with the Spinner
is most important thing.
在代码中,使用与Spinner相同的上下文是最重要的。
spinner.getContext()
#10
2
Simplest: Works for me
简单:适合我
TextView spinnerText = (TextView) spinner.getChildAt(0);
spinnerText.setTextColor(Color.RED);
#11
2
The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.
重复使用/更改android的最简单方法。布局资源就是定义。在Android Studio中,对Android . r .layout.simple_spinner_item.xml进行Ctrl + B操作。
It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:
它将带您到资源文件。只需复制资源文件,并在Package.R中添加新的布局。布局文件夹,并更改textview的textColor,然后在适配器中这样调用:
ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
#12
1
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
just use this:
就用这个:
ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
R.layout.spinner_list_item, categories);
adapter_category
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
#13
1
First we have to create the simple xml
resource file for the textview
like as below:
首先,我们必须为textview创建一个简单的xml资源文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
and save it. after set on your adapterlist.
并保存它。在您的适配器列表上设置之后。
#14
1
For those who want to change DrowDownIcon
color you can use like this
对于那些想要改变昏昏欲睡图标颜色的人,你可以这样使用。
spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
#15
0
Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:
Ashraf解决方案的另一种变体是确保考虑到屏幕大小。在设置适配器之后,您需要在onCreate中获取spinner并设置侦听器:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
Then you can start changing the text size of the view that's showing before the spinner is clicked:
然后你可以开始改变视图的文本大小,这是在spinner被点击之前显示的:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
All you need to do is create layout specific folders like this:
你所需要做的就是创建这样的特定于布局的文件夹:
values-sw360dp
values-sw360dp
values-sw600dp
values-sw600dp
values-sw800dp
values-sw800dp
an then add an xml file named "bool.xml" into each of those folders:
然后添加一个名为“bool”的xml文件。将xml“放入每个文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
#16
0
I have done this as following.I have use getDropDownView() and getView() methods.
我是这样做的。我使用了getDropDownView()和getView()方法。
Use getDropDownView()
for opened Spinner.
对打开的微调器使用getDropDownView()。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView()
for closed Spinner.
并使用getView()作为闭微调器。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
#17
0
you can have this type of adapter for spinner, totally customized:
您可以使用这种适配器来进行微调,完全定制:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
while R.layout.spinner_text is:
而出来。spinner_text是:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="@color/whiteThree" />
#18
0
To change the color of spinner text :
改变旋转文字的颜色:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
#19
-2
Try this method. It is working for me.
试试这个方法。它在为我工作。
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
((TextView) adapterView.getChildAt(0)).setTextSize(20);
Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}
#1
611
Make a custom XML file for your spinner item.
为微调项创建自定义XML文件。
spinner_item.xml:
spinner_item.xml:
Give your customized color and size to text in this file.
将您的自定义颜色和大小发送到该文件中的文本。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
现在使用这个文件来显示你的微调项,比如:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set the drop down resource. It will take spinner_item.xml
only to show your items in spinner.
您不需要设置下拉资源。它需要spinner_item。xml只显示您的项目在旋转。
#2
153
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
119
If all the spinners may have the same text color for their TextView items, another approach is to use a custom style for spinner dropdown items:
如果所有的旋转器的TextView项都有相同的文本颜色,另一种方法是为spinner下拉项使用自定义样式:
In res/values/styles.xml
:
在res /价值/ styles.xml:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
</style>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textColor">@color/my_spinner_text_color</item>
</style>
</resources>
And define your custom color in res/values/colors.xml:
并在res/values/colors.xml中定义自定义颜色。
<color name="my_spinner_text_color">#808080</color>
#4
56
Here is a link that can help you to change the color of the Spinner:
这里有一个链接可以帮助你改变旋转器的颜色:
点击这里
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:textSize="20sp"
android:entries="@array/planets"/>
You need to create your own layout file with a custom definition for the spinner item spinner_item.xml:
您需要为spinner项目spinner_item.xml创建具有自定义定义定义的布局文件:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
If you want to customize the dropdown list items, you will need to create a new layout file. spinner_dropdown_item.xml:
如果您想要定制下拉列表项,您需要创建一个新的布局文件。spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
And finally another change in the declaration of the spinner:
最后,旋转器声明的另一个变化是
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
That's it.
就是这样。
#5
14
To prevent lagging, you need to not only set the text properties in the onItemSelected
listener, but also in the Activity's onCreate
method (but it's a little tricky).
为了防止滞后,您不仅需要在onItemSelected侦听器中设置文本属性,还需要在活动的onCreate方法中设置文本属性(但这有点棘手)。
Specifically, you need to put this in onCreate
after setting the adapter:
具体来说,您需要在设置适配器后将其放入onCreate:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected
:
然后把这个放到onItemSelected:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
这里有一个完整的例子:
@Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.
有关更多细节,请参见我的问题。
#6
10
If you want the text color to change in the selected item only, then this can be a possible workaround. It worked for me and should work for you as well.
如果您希望文本颜色只在选定的项目中更改,那么这可能是一个解决方案。它对我有用,也应该对你有用。
spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE);
}
});
#7
5
Rather than making a custom layout to get a small size and if you want to use Android's internal small size LAYOUT for the spinner, you should use:
与其定制一个小尺寸的布局,如果你想使用Android的内部小尺寸的布局,你应该使用:
"android.R.layout.simple_gallery_item" instead of "android.R.layout.simple_spinner_item".
“android.R.layout。simple_gallery_item”而不是“android.R.layout.simple_spinner_item”。
ArrayAdapter<CharSequence> madaptor = ArrayAdapter
.createFromResource(rootView.getContext(),
R.array.String_visitor,
android.R.layout.simple_gallery_item);
It can reduce the size of spinner's layout. It's just a simple trick.
它可以减少纺纱机布局的大小。这只是一个简单的技巧。
If you want to reduce the size of a drop down list use this:
如果你想减少下拉列表的大小,请使用以下方法:
madaptor.setDropDownViewResource(android.R.layout.simple_gallery_item);
#8
5
If you work with android.support.v7.widget.AppCompatSpinner here is the simplest tested solution using styles:
如果你使用android.support.v7.widget。AppCompatSpinner是使用样式测试的最简单的解决方案:
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spefcialFx"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:theme="@style/Spinner"
android:entries="@array/special_fx_arrays"
android:textSize="@dimen/text_size_normal"></android.support.v7.widget.AppCompatSpinner>
And the style:
和风格:
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/white</item>
<item name="android:backgroundTint">@color/red</item>
<item name="android:textSize">14sp</item>
</style>
The only downside is the android:backgroundTint sets color for both the dropdown arrow and the dropdown background.
唯一的缺点是android:backgroundTint为下拉箭头和下拉背景设置颜色。
#9
3
For someone who needs only Style
way for AppCompat
.
对于那些只需要使用AppCompat的方式的人。
结果
styles.xml
styles.xml
<resources>
...
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">@color/material_grey_700</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
your_spinner_layout.xml
your_spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/content_spinner"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:entries="@array/shipping_tracking_carrier_names"
android:spinnerMode="dropdown"
android:theme="@style/Spinner" />
<EditText
android:id="@+id/content_input"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:textColor="@color/material_grey_700"
android:textSize="12sp" />
...
</LinearLayout>
Plus
And if you want to set android:entries
programmatically with defined style.
Try this.
另外,如果您想要设置android:以编程方式定义样式的条目。试试这个。
AppCompatSpinner spinner = findViewById(R.id.content_spinner);
CharSequence[] entries = getResources().getTextArray(R.array.shipping_tracking_carrier_names);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(spinner.getContext(), android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
As in the code, using same Context
with the Spinner
is most important thing.
在代码中,使用与Spinner相同的上下文是最重要的。
spinner.getContext()
#10
2
Simplest: Works for me
简单:适合我
TextView spinnerText = (TextView) spinner.getChildAt(0);
spinnerText.setTextColor(Color.RED);
#11
2
The easiest way to re-use/change the android.R.layout resources is just go the definition. In Android Studio, do Ctrl + B on android.R.layout.simple_spinner_item.xml.
重复使用/更改android的最简单方法。布局资源就是定义。在Android Studio中,对Android . r .layout.simple_spinner_item.xml进行Ctrl + B操作。
It will take you to the resource file. Just copy the resource file and add a new layout in your Package.R.layout folder and change the textColor of textview as you like and then just call it in adapter like this:
它将带您到资源文件。只需复制资源文件,并在Package.R中添加新的布局。布局文件夹,并更改textview的textColor,然后在适配器中这样调用:
ArrayAdapter<String> adapter = new ArrayAdapter<String(Context,R.layout.spinner_item, spinnerlist);
#12
1
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
just use this:
就用这个:
ArrayAdapter<String> adapter_category = new ArrayAdapter<String>(this,
R.layout.spinner_list_item, categories);
adapter_category
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
#13
1
First we have to create the simple xml
resource file for the textview
like as below:
首先,我们必须为textview创建一个简单的xml资源文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
and save it. after set on your adapterlist.
并保存它。在您的适配器列表上设置之后。
#14
1
For those who want to change DrowDownIcon
color you can use like this
对于那些想要改变昏昏欲睡图标颜色的人,你可以这样使用。
spinner.getBackground().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
#15
0
Another variation of Ashraf's solution would be to make sure you're taking into account screen sizes. You'll need to get the spinner in onCreate and set the listener after you set the adapter:
Ashraf解决方案的另一种变体是确保考虑到屏幕大小。在设置适配器之后,您需要在onCreate中获取spinner并设置侦听器:
//set your adapter with default or custom spinner cell, then://
serverSpinner.setOnItemSelectedListener(spinnerSelector);
serverSpinner.setSelection(defaultServer);
Then you can start changing the text size of the view that's showing before the spinner is clicked:
然后你可以开始改变视图的文本大小,这是在spinner被点击之前显示的:
private AdapterView.OnItemSelectedListener spinnerSelector = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
boolean largeTablet = getResources().getBoolean(R.bool.isLargeTablet);
if (tabletSize) { ((TextView)parent.getChildAt(0)).setTextSize(16); }
else if (largeTablet) { ((TextView)parent.getChildAt(0)).setTextSize(18); }
else { ((TextView)parent.getChildAt(0)).setTextSize(12); }
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
All you need to do is create layout specific folders like this:
你所需要做的就是创建这样的特定于布局的文件夹:
values-sw360dp
values-sw360dp
values-sw600dp
values-sw600dp
values-sw800dp
values-sw800dp
an then add an xml file named "bool.xml" into each of those folders:
然后添加一个名为“bool”的xml文件。将xml“放入每个文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLargeTablet">false</bool>
</resources>
#16
0
I have done this as following.I have use getDropDownView() and getView() methods.
我是这样做的。我使用了getDropDownView()和getView()方法。
Use getDropDownView()
for opened Spinner.
对打开的微调器使用getDropDownView()。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView()
for closed Spinner.
并使用getView()作为闭微调器。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
#17
0
you can have this type of adapter for spinner, totally customized:
您可以使用这种适配器来进行微调,完全定制:
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_text, genderList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#676767"));
((TextView) v).setTypeface(vrFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTypeface(vrFont);
((TextView) v).setTextColor(Color.parseColor("#676767"));
if (position == 0) {
((TextView) v).setTextColor(Color.parseColor("#979797"));
}
return v;
}
while R.layout.spinner_text is:
而出来。spinner_text是:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical|left"
android:ellipsize="marquee"
android:maxLines="1"
android:textColor="@color/whiteThree" />
#18
0
To change the color of spinner text :
改变旋转文字的颜色:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);}
#19
-2
Try this method. It is working for me.
试试这个方法。它在为我工作。
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
((TextView) adapterView.getChildAt(0)).setTextColor(Color.RED);
((TextView) adapterView.getChildAt(0)).setTextSize(20);
Toast.makeText(this, textView.getText()+" Selected", Toast.LENGTH_SHORT).show();
}