[英]Change popupbackground color of dialog spinner
我想更改对话框微调器的popupBackground颜色。
在我的activity.xml
:
<Spinner
android:id="@+id/mCategorySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView7"
android:entries="@array/recipeCategory"
android:spinnerMode="dialog"
android:popupBackground="@color/colorPrimary"
android:textAlignment="center" />
在我的activity.java
:
categorySpinner=(Spinner) findViewById(R.id.mCategorySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.recipeCategory, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setPrompt("Choose category");
categorySpinner.setAdapter(new NothingSelectedSpinnerAdapter(
adapter,
R.layout.category_spinner_row_nothing_selected,
this));
如果我以XML更改android:popupBackground
,则没有任何android:popupBackground
,它仍然是默认的白色。
但是,如果我更改背景,它将起作用,但不适用于对话框的背景。
您可以像这样改变背景颜色和下拉图标
步骤1:在drawable文件夹中,为微调框的背景创建一个名为background.xml
的文件。
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="@color/darkGray" />
</shape>
步骤2:现在将此背景应用于xml文件中的微调器
android:background="@drawable/background"
1,使用spinner_selector.xml
显示您更改的颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/holo_red_light"
android:state_pressed="true"/>
<item android:color="@android:color/white"
android:state_pressed="false"/>
</selector>
2.添加样式
将其添加到样式中,即可在其他位置使用它。
<style name="spinner_style">
<item name="android:background">@drawable/spinner_selector</item>
</style>
3.将其添加到xml代码中
使用它作为微调器的背景。
<Spinner
android:id="@+id/mCategorySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView7"
android:entries="@array/recipeCategory"
android:spinnerMode="dialog"
style="@style/spinner_style"
android:textAlignment="center" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.