Android Spinner对话框弹出背景
我的Android应用程序已将popupbackground设置为可绘制的xml.但是,弹出对话框仍然无法显示我设置的颜色.如何解决这个问题?
My android app already set popupbackground as drawable xml. However, the popup dialog still cannot show the color I set. How to settle this issue?
<?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:orientation="vertical"
tools:context=".CountrySelectorActivity">
<Spinner
android:id="@+id/search_spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:popupBackground="@drawable/spinner_background"
android:spinnerMode="dialog"
/>
<Spinner
android:id="@+id/search_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@color/black"
android:popupBackground="@drawable/spinner_background"
android:spinnerMode="dialog"/>
</LinearLayout>
@ drawable/spinner_background.xml
@drawable/spinner_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/green"/>
</shape>
</item>
</selector>
使用自定义微调器背景可绘制对象制作样式.然后将样式作为属性添加到微调器.最后,以编程方式更改活动或片段中的微调框弹出背景颜色.以下方法对我有用:
Make a style using your custom spinner background drawable. Then add the style as an attribute to your spinner. Lastly, programmatically change the spinner popup background color in your activity or fragment. The following way worked for me:
<style name="SpinnerTheme" parent="android:Widget.DeviceDefault.Spinner">
<item name="android:background">@drawable/spinner_background</item>
<item name="android:padding">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">@android:color/white</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:paddingRight">15dp</item>
</style>
为此在每个转盘中添加XML(删除android:popupBackground ="@ drawable/spinner_background"&android:spinnerMode ="dialog"):
Put this in your xml for each spinner (REMOVE android:popupBackground="@drawable/spinner_background" & android:spinnerMode="dialog"):
<Spinner
android:id="@+id/search_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@color/black"
style="@style/SpinnerTheme"/>
然后在您的活动或片段中,以编程方式设置弹出式背景颜色:
Then in your activity or fragment, programmatically set the popup background color:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spinner.setPopupBackgroundResource(R.color.yourColor);
}
这是示例的链接: https://www.oodlestechnologies.com/blogs/Android中的Custom-Spinner-in