我正在制作Spinner
,如下图所示。它可以正常工作。但我想将background
颜色更改为#FFFFFF
,将textColor
更改为#000000
。但我得到了修改输出...
java代码:
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
xml代码:
<Spinner
android:id="@+id/static_spinner"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_marginLeft="30dp"
style="@android:style/Widget.Holo.Light.DropDownItem" ></Spinner>
simple_spinner_dropdown_item.xml
<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:textColor="#000000"
/>
simple_spinner_item.xml
<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:ellipsize="marquee"
android:textColor="#000000"
android:textAlignment="inherit"/>
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#0072BA</item>
<item name="colorPrimaryDark">#004F80</item>
<item name="colorAccent">#0072BA</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
<item name="android:actionBarSize">48dp</item>
<item name="actionBarSize">48dp</item>
<item name="android:windowActionBar">false</item>
</style>
</resources>
答案 0 :(得分:2)
在代码中使用此微调器
<Spinner
style="@style/edit_style"
android:id="@+id/spr_city"
android:background="@drawable/selector_spinner"
android:layout_width="0dp"
android:textColor="@android:color/white"
android:minHeight="@dimen/min_height"
android:textCursorDrawable="@null"
android:textColorHint="@android:color/white"
android:layout_height="fill_parent"
android:layout_weight="9"
/>
并以风格.xml
制作此样式 <style name="edit_style" >
<item name="android:textSize">14sp</item>
<item name="android:textColor">#ffffff</item>
</style>
并在活动中使用此代码
ArrayAdapter<String> adp1=new ArrayAdapter<String> (activity,R.layout.spinner_item_selected,city_list);
adp1.setDropDownViewResource(R.layout.spinner_item);
s_city.setAdapter(adp1);
并创建xml spinner_item_selected
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/common_margin"
style="@style/edit_style"
android:textColor="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" />
还有一个xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textViewSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/common_margin"
style="@style/edit_style"
android:textColor="@color/spinner_item_selector"
xmlns:android="http://schemas.android.com/apk/res/android" />
答案 1 :(得分:1)
您可以在xml中设置旋转器背景颜色,如下所示:
android:background="YOUR_HEX_COLOR_CODE"
如果您使用下拉菜单和旋转器,您可以设置它的背景颜色:
android:popupBackground="YOUR_HEX_COLOR_CODE"
答案 2 :(得分:1)
通过使用以下代码,我可以解决我的问题。
<Spinner
android:id="@+id/static_spinner2"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:layout_marginLeft="30dp"
android:background="@drawable/apptheme_spinner_background_holo_light"
android:popupBackground="#ffffff" />
<强> 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="14sp"
android:background="#FFFFFF"
android:textColor="#000000" />
<强> 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:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"/>
java代码:
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,R.layout.spinner_item);
staticAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
答案 3 :(得分:0)
将android.R更改为yourprojcet.R
android.R.layout.simple_spinner_dropdown_item
到
letmobility.com.itforte.R.layout.simple_spinner_dropdown_item;
它采用了Android布局,而不是你的应用程序特定布局。
答案 4 :(得分:0)
你的电话应该是这样的:
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
而不是:
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter.createFromResource(this,R.array.request_role,android.R.layout.simple_spinner_item);
staticAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);