2015-12-10 255 views
1

我想将Spinner下拉项目的文本颜色更改为黑色。如何更改Spinner下拉项目的文字颜色?

我有一个自定义的微调列表,列出不同的语言。它有一个ImageView和一个TextView。微调器显示在白色文字的蓝色背景上,看起来很好。

问题是当它被点击时,下拉菜单显示,但由于下拉的浅灰色和文本的白色,文本几乎不可见。

这是activity_main.xml的微调代码:

<Spinner 
    android:id="@+id/languageSpinner" 
    style="@android:style/Widget.Material.Light.DropDownItem.Spinner" 
    android:layout_width="140dp" 
    android:layout_height="40dp" 
    android:prompt="@string/selectLanguageSpinner" 
    android:spinnerMode="dropdown" /> 

这是spinner_row_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 

     <TextView 
      android:id="@+id/language" 
      android:layout_width="wrap_content" 
      android:layout_height="40dp" 
      android:layout_gravity="center_vertical" 
      android:layout_marginEnd="10dp" 
      android:layout_marginStart="5dp" 
      android:gravity="center_vertical" 
      android:lineSpacingExtra="20dp" 
      android:minHeight="40dp" 
      android:textColor="@color/white" 
      android:textSize="20sp" /> 
    </LinearLayout> 
</LinearLayout> 

下面的文本颜色设置为白色,因为我需要它是白色的什么都没有选择。

这是styles.xml代码:

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/myAppPrimaryColor</item> 
     <item name="colorPrimaryDark">@color/myAppPrimaryDarkColor</item> 
     <item name="colorAccent">@color/myAppAccentColor</item> 
     <item name="android:dropDownListViewStyle">@style/TestSpinnerStyle</item> 
     <item name="android:spinnerItemStyle">@style/mySpinnerItemStyle</item> 
    </style> 

    <style name="mySpinnerItemStyle"> 
     <item name="android:paddingLeft">5dp</item> 
     <item name="android:paddingBottom">3dp</item> 
     <item name="android:textSize">20sp</item> 
     <item name="android:textColor">@color/white</item> 
    </style> 

    <!-- ^^ Here I need to set the color to white, because I also have default 
      spinner on the activity that needs to have a default white color. 
      But when the default spinner is clicked, the color of the 
      drop down items is black. How can I do that with my custom spinner? --> 

    <style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown"> 
     <item name="android:textSize">22sp</item> 
     <item name="android:textColor">@color/black</item> 
     <item name="android:height">20dp</item> 
     <item name="android:divider">@color/gray</item> 
     <item name="android:dividerHeight">1dp</item> 
     <item name="android:dividerPadding">5dp</item> 
     <item name="android:background">@color/default_gray</item> 
    </style> 

    <!-- ^^ The text color property here is not reflected.. --> 

没有任何人有任何建议,如何做到这一点?

编辑:在评论和答案的帮助下,我设法做到了。我认为它可以在XML中完成...反正在后面的java代码中,这是我所做的:

当定义自定义适配器时,我们需要重写getDropDownView方法,当微调器点击并显示下拉菜单。 getView方法也应该被覆盖,所以我们可以在没有点击Spinner时处理默认颜色。换句话说,这里是代码:

public class CustomSpinnerAdapter extends ArrayAdapter<String> { 
     public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, TypedArray image){ 
      super(context, textViewResourceId, objects); 
     } 

     @Override 
     public View getDropDownView(int position, View convertView, ViewGroup parent){ 
      LayoutInflater inflater = getLayoutInflater(); 
      View row = inflater.inflate(R.layout.spinner_row_layout, parent, false); 

      TextView label = (TextView) row.findViewById(R.id.language); 
      label.setText(languages_list[position]); 

      label.setTextColor(getResources().getColor(android.R.color.black)); 

      ImageView icon = (ImageView) row.findViewById(R.id.icon); 
      icon.setImageDrawable(icons_list.getDrawable(position)); 

      return row; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent){ 
      LayoutInflater inflater = getLayoutInflater(); 
      View row = inflater.inflate(R.layout.spinner_row_layout, parent, false); 

      TextView textView = (TextView) row.findViewById(R.id.language); 
      textView.setText(languages_list[position]); 
      textView.setTextColor(getResources().getColor(android.R.color.white)); 

      ImageView icon = (ImageView) row.findViewById(R.id.icon); 
      icon.setImageDrawable(icons_list.getDrawable(position)); 

      return row; 
     } 
    } 

注意,在getDropDownViewTextView的颜色设置为黑色,而在getView它设置为白色。

+0

您正在使用自己的实现适配器用于显示微调的项目? – flyingAssistant

+0

@flyingAssistant我已经完成了这个教程:http://www.edureka.co/blog/custom-spinner-in-android/ – Apostrofix

回答

4

我已经做了的事情这样的,但在BaseAdapter

public View getDropDownView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 
      if (view == null) { 
       view = inflater.inflate(R.layout.spinner_item, null); 
      } 

      TextView txtTestText = (TextView) view.findViewById(R.id.txtSpinnerRowText); 
      txtTestText.setText(list.get(position).getText()); 
      if (getSelectedPosition() == position) { 
       txtTestText.setTextColor(context.getResources().getColor(android.R.color.holo_blue_dark)); 
      } else { 
       txtTestText.setTextColor(context.getResources().getColor(android.R.color.primary_text_light)); 
      } 
      return view; 
     } 
+0

getSelectedPosition()是什么? – Apostrofix

+0

这是所选'Spinner'行项目的索引,在'BaseAdapter'中定义为字段。每当选择“Spinner”下拉菜单项(“OnItemSelectedListener.onItemSelected”)或未选中(“OnItemSelectedListener.onNothingSelected”)时,我都会更改此索引。 – flyingAssistant

+0

谢谢,我设法做到了。我将用完整的解决方案编辑我的问题。我能够改变'getDropDownView'和'getView'中的颜色,所以现在看起来很好。 – Apostrofix

0

试试这个我希望不行。

((Spinner)findViewById(R.id.spinnergender)).setAdapter(adapter); 
     ((Spinner)findViewById(R.id.spinnergender)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       Log.e("Value",""+position); 
       ((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.cyan)); 
       if(parent.getItemAtPosition(position).toString().equals(getResources().getString(R.string.Gender))) { 
        ((TextView) ((Spinner)findViewById(R.id.spinnergender)).getSelectedView()).setTextColor(getResources().getColor(R.color.Text)); 
       } 

      } 
0

您应该创建自定义适配器(延伸BaseAdapter)的微调和getDropDownView膨胀定制spinner_item_dropdown.xml,它被设置为你的愿望。

相关问题