有什么简单的方法可以更改 Android 中的 Spinner 下拉菜单颜色?
Is there any easy way to change Spinner dropdown color in Android?
我创建了与应用程序一起使用的主题,主题的父主题是 Theme.AppCompat.Light.NoActionBar
顺便说一句,我想要白底黑字。
这是适配器代码
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
child.spinner.adapter = adapter
有什么简单的方法可以更改 Android 中的 Spinner 下拉菜单颜色?
是的。您可以在 xml
中使用以下属性来回旋转器
android:popupBackground="YOUR_HEX_COLOR_CODE"
更改文本颜色等 为您的微调项创建自定义 XML 文件。
spin_item.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="24sp"
android:textColor="#000000"
android:padding="4dp"
/>
然后像这样使用它:
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(R.layout.spin_item)
通过代码
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
或通过XML
对于 API 21+:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/red" />
或者如果你使用支持库,你可以使用:
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/red" />
在您的代码中,在您的 onCreate() 中添加以下内容:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red),
PorterDuff.Mode.SRC_ATOP);
创建一个如下所示的新布局文件
<?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:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:background="MY REQUIRED COLOR"/>
在我说的地方MY REQUIRED COLOR
请设置成你想要的颜色
还要确保您没有更改 android:id
属性,因为 arrayadapter 将使用它来将文本设置为 textview
然后在创建过程中将其设置为您的数组适配器
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, .R.layout.custom_ simple_spinner_item)
location_Survey_Spin = findViewById(R.id.location_Survey_Spinner);
location_Survey_Spin.getBackground().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP); //它将改变微调器下拉颜色
要更改 下拉菜单背景颜色,请在 xml 文件中为您的 Spinner
小部件使用 android:popupBackground="@color/aColor"
:
<Spinner
android:id="@+id/my_spinner"
android:layout_width="100dp"
android:layout_height="match_parent"
android:popupBackground="@color/aColor" />
在您的 styles.xml
文件中使用浅色主题时, 微调器下拉图标颜色 将为黑色,但请注意,如果您使用的是 <item name="android:textColorSecondary">@color/aColor</item>
下拉图标将选择该颜色:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorSecondary">@color/aColor</item>
即使你的问题是要更改 下拉菜单背景颜色 我来这里是因为我想了解为什么我的 spinner 下拉图标颜色 在我发现 (android:textColorSecondary
) 之前一直使用不同的颜色 - 所以希望这也能帮助其他人。
我创建了与应用程序一起使用的主题,主题的父主题是 Theme.AppCompat.Light.NoActionBar
顺便说一句,我想要白底黑字。
这是适配器代码
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
child.spinner.adapter = adapter
有什么简单的方法可以更改 Android 中的 Spinner 下拉菜单颜色?
是的。您可以在 xml
中使用以下属性来回旋转器android:popupBackground="YOUR_HEX_COLOR_CODE"
更改文本颜色等 为您的微调项创建自定义 XML 文件。
spin_item.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="24sp"
android:textColor="#000000"
android:padding="4dp"
/>
然后像这样使用它:
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(R.layout.spin_item)
通过代码
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
或通过XML
对于 API 21+:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/red" />
或者如果你使用支持库,你可以使用:
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="@color/red" />
在您的代码中,在您的 onCreate() 中添加以下内容:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red),
PorterDuff.Mode.SRC_ATOP);
创建一个如下所示的新布局文件
<?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:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:background="MY REQUIRED COLOR"/>
在我说的地方MY REQUIRED COLOR
请设置成你想要的颜色
还要确保您没有更改 android:id
属性,因为 arrayadapter 将使用它来将文本设置为 textview
然后在创建过程中将其设置为您的数组适配器
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, .R.layout.custom_ simple_spinner_item)
location_Survey_Spin = findViewById(R.id.location_Survey_Spinner); location_Survey_Spin.getBackground().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP); //它将改变微调器下拉颜色
要更改 下拉菜单背景颜色,请在 xml 文件中为您的 Spinner
小部件使用 android:popupBackground="@color/aColor"
:
<Spinner
android:id="@+id/my_spinner"
android:layout_width="100dp"
android:layout_height="match_parent"
android:popupBackground="@color/aColor" />
在您的 styles.xml
文件中使用浅色主题时, 微调器下拉图标颜色 将为黑色,但请注意,如果您使用的是 <item name="android:textColorSecondary">@color/aColor</item>
下拉图标将选择该颜色:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorSecondary">@color/aColor</item>
即使你的问题是要更改 下拉菜单背景颜色 我来这里是因为我想了解为什么我的 spinner 下拉图标颜色 在我发现 (android:textColorSecondary
) 之前一直使用不同的颜色 - 所以希望这也能帮助其他人。