在Spinner中,当选择特定项目时,背景不会改变

时间:2016-07-18 10:23:45

标签: android android-layout spinner android-spinner

1 个答案:

答案 0 :(得分:1)

首先在名为<{p>的drawable下创建drawable folder

  

my_click_changer

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/your_new_Color" android:state_pressed="true" />
    <item android:drawable="@color/your_default_Color" android:state_pressed="false" />
</selector>

编辑这是为了更改text color

创建另一个drawable

  

text_color_changer

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:color="@android:color/black" android:state_checked="true" />
   <item android:color="@android:color/white" />
 </selector>

然后为每个Spinner项创建一个单独的布局,

  

your_xml_name.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   style="?android:attr/spinnerItemStyle"
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:background="@drawable/my_click_changer"
   android:ellipsize="marquee"
   android:gravity="center"
   android:padding="10dp"
   android:singleLine="true"
   android:textAlignment="inherit"
   android:textSize="15sp"
   android:textColor="@drawable/text_color_changer"
   android:textStyle="bold" />
  

在代码中按

创建Adapter
    final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(), R.layout.your_xml_name, yourList);