【问题标题】:Change text color of selected item in spinner更改微调器中所选项目的文本颜色
【发布时间】:2013-03-01 01:34:08
【问题描述】:

如何在微调器中更改所选项目的字体颜色?

我可以更改所选项目的背景颜色、下拉项目的颜色等,但不能更改所选项目的文本颜色...我该怎么做?

我的代码是: 这是我正在使用的微调器--:

<Spinner
                android:id="@+id/spinner1"
                android:layout_width="wrap_content"
                android:layout_height="32dip"
                android:background="@drawable/mybg"
                android:divider="@drawable/list_divider"
                android:drawSelectorOnTop="true"
                android:popupBackground="#D3D5D3"
                android:prompt="@string/activityy_prompt" 
                />

这是 mybg.xml

<!-- <item android:drawable="@drawable/blue" android:state_pressed="false"/> -->
<!-- <item android:drawable="@drawable/back11"/> -->

<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/greenyellow1" android:state_selected="true"/>
<item android:drawable="@drawable/back11"/>

使用这些我无法更改所选项目的文本颜色...

【问题讨论】:

  • 使用这个链接,它会帮助stackoverflow.com/questions/7584158/…
  • 将您尝试过的代码提供给我们。没有硬币没有电话
  • @user1283633 我点击此链接,只能更改背景颜色....无法更改文本颜色...

标签: android android-spinner


【解决方案1】:

像这样定义OnItemSelectedListener

  private AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

然后将OnItemSelectedListener 设置为spinner,如下所示:

spinner.setOnItemSelectedListener(listener);

【讨论】:

  • Thx... 它的工作.. 早些时候它不工作但现在它的工作可能是因为我在清单文件中添加了几行...... android:configChanges="orientation|keyboardHidden|screenLayout |screenSize"
  • 这可能会导致设置默认选择的颜色有点滞后。请在此处查看我的答案以解决该问题:stackoverflow.com/questions/9476665/…
【解决方案2】:

可绘制/mybg:

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

这将改变弹出窗口中所选项目的颜色。

【讨论】:

  • 更改 android:drawable="@color/red"
【解决方案3】:

尝试在 OnItemSelectedListener 中实现 onItemSelected 以更改微调器所选项目的文本颜色

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
    int index = adapterView.getSelectedItemPosition();
    ((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<----

【讨论】:

    【解决方案4】:

    您可以通过将OnItemSelectedListener 添加到spinner 来更改选定的文本颜色

    qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) view).setTextColor(Color.BLACK); //Change selected text color
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    

    【讨论】:

    • 太棒了!添加这个以更改文本大小: ((TextView) view).setTextSize(30f);
    • 以编程方式工作得非常好!谢谢!
    • 不幸的是,您可以看到从默认颜色切换到新颜色的延迟。
    【解决方案5】:

    使用选择器作为文本颜色。

    在drawable中创建color_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:state_pressed="true"
               android:color="#000000" /> <!-- pressed -->
         <item android:state_focused="true"
               android:color="#000000" /> <!-- focused -->
         <item android:color="#FFFFFF" /> <!-- default -->
     </selector>
    

    在文本视图中

    <TextView 
       android:textColor="@drawable/color_selector"/>
    

    【讨论】:

    • 很奇怪。这里与背景无关,您需要做的就是使用 android:textColor=""@drawable/color_selector" 和 view contains text (spinner/textView) 。
    • spinner 有属性 textcolor 吗??
    • 希望如此。否则,您将需要使用 textview 和 spinner 来实现这种方法。
    • 这是一个非常优雅的解决方案。
    • 我用它,运行应用时,应用崩溃
    【解决方案6】:

    只需使用这一行 onItemSelected listner -

    public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,long arg3) 
         {
    
           item = (String) parent.getItemAtPosition(arg2);
    
    
           ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
    
     }
    

    【讨论】:

      【解决方案7】:

      onItemSelected(...) 监听器方法中设置((TextView) view).setTextColor(getResources().getColor(R.color.black)); 将起作用。 它为您选择的微调器文本设置颜色。

      【讨论】:

        【解决方案8】:

        如果您的微调器使用 ArrayAdapter,您可以简单地为微调器项传递自定义布局。该布局可以是一个简单的 textView,但具有 android:textColor 属性。

        MainActivity.java onCreate()

        adapter = new ArrayAdapter<>(this, R.layout.spinner_custom_textcolor, data);
        spinner.setAdapter(adapter);
        

        spinner_custom_textcolor.xml

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerItemStyle" 
        android:textColor="@color/YOUR_COLOR_HERE"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:textAlignment="inherit"/>
        

        *上述布局中除了 android:textColor 之外的所有内容都是从 android.R.layout.simple_spinner_item

        直接复制的

        【讨论】:

          【解决方案9】:

          我知道这是个老问题,但我在 TabLayout 的微调器中更改所选项目的颜色时遇到了很大问题,这对我来说真的很管用:

          spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                      @Override
                      public void onGlobalLayout() {
                          ((TextView) spinner.getSelectedView()).setTextColor(Color.WHITE); //change to your color
                      }
                  });
          

          【讨论】:

            【解决方案10】:

            你们中的一些人使用 MaterialBetterSpinner 并绑定你的布局, 以上都无济于事,试试这个,希望对你有帮助:

            public class MyAdapter extends ArrayAdapter<String> {      
            
                    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
                        super(context, textViewResourceId, objects);           
            
                    }
            
                    @Override
                    public View getDropDownView(int position, View convertView, ViewGroup parent) {
                        return getCustomView(position, convertView, parent);
                    }
            
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        return getCustomView(position, convertView, parent);
                    }
            
                    public View getCustomView(int position, View convertView, ViewGroup parent) {
                        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false);
                        rowBinding.tv1.setText(mMy.getValues().get(position));
                        if(position == mMy.getCurrentIndex()) {
                            rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font
                            rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color
                        }
                        return rowBinding.getRoot();
                    }
                }
            

            yourXML 是这样的:

            <?xml version="1.0" encoding="utf-8"?>
            <layout>
            
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:background="@color/colorBackgroundStart">
                    <TextView
                        android:id="@+id/tv1"
                        android:layout_width="0dp"
                        android:layout_weight="0.7"
                        android:layout_height="30dp"
                        android:textColor="#fff"
                        android:textSize="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="8dp"/>
            
            </layout>
            

            使用此适配器和 yourXML 创建一个微调器:

            final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
            
            final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
            spinner.setAdapter(adapter);
            

            【讨论】:

              【解决方案11】:

              查看我对类似问题的回答here。我的答案与 Priya 的类似,除了它也正确设置了默认选定项目的文本颜色(因此在等待微调器自动选择默认项目时不会出现错误颜色的滞后,这发生在用户界面已经打开之后 -屏幕)。

              【讨论】:

                【解决方案12】:

                在 Android 2.3v 中更改背景颜色不需要 java 代码。只需添加 android:background="#F0F8FF" xml 文件中的微调器。

                【讨论】:

                  【解决方案13】:
                  mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                             ((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
                  }
                  

                  【讨论】:

                    【解决方案14】:

                    对我有用的解决方案是为下拉资源视图使用CheckedTextView,然后使用颜色选择器更改选中项目的颜色。

                    spinner_dropdown_item.xmllayout 文件夹中:

                    <?xml version="1.0" encoding="utf-8"?>
                    
                    <!-- A `CheckedTextView` allows the color of the text to be changed when it is selected (checked). -->
                    <CheckedTextView
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/spinner_item_textview"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:maxLines="1"
                        android:ellipsize="end"
                        android:paddingStart="20dp"
                        android:paddingEnd="20dp"
                        android:paddingTop="8dp"
                        android:paddingBottom="8dp"
                        android:textSize="18sp"
                        android:textColor="@color/spinner_color_selector"
                        android:background="@color/spinner_background" />
                    

                    spinner_color_selectorcolor 文件夹中:

                    <?xml version="1.0" encoding="utf-8"?>
                    
                    <!-- Highlight the selected (checked) item when the spinner is open. -->
                    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
                        <item android:state_checked="true" android:color="@color/white" />
                        <item android:color="@color/blue_100" />
                    </selector>
                    

                    spinner_dropdown_item.xml 必须设置为 DropDownResourceView 用于 Adapter。就我而言,我使用的是从多个来源获取信息的ResourceArrayAdapter

                    // Setup a `MatrixCursor` for the static entries.
                    String[] matrixCursorColumnNames = {DatabaseHelper._ID, DatabaseHelper.NAME};
                    MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
                    matrixCursor.addRow(new Object[]{-2, getString(R.string.first_spinner_item)});
                    matrixCursor.addRow(new Object[]{-1, getString(R.string.second_spinner_item)});
                    
                    // Get a `Cursor` with the list of additional items from the database.
                    Cursor cursor = DatabaseHelper.getCursor();
                    
                    // Combine `matrixCursor` and `cursor`.
                    MergeCursor mergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
                    
                    // Create a `ResourceCursorAdapter` for the spinner with `this` context.  `0` specifies no flags.;
                    ResourceCursorAdapter resourceCursorAdapter = new ResourceCursorAdapter(this, R.layout.spinner_item, mergeCursor, 0) {
                        @Override
                        public void bindView(View view, Context context, Cursor cursor) {
                            // Get a handle for the spinner item `TextView`.
                            TextView spinnerItemTextView = (TextView) view.findViewById(R.id.spinner_item_textview);
                    
                            // Set the `TextView` to display the name.
                            spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.NAME)));
                        }
                    };
                    
                    // Set the `ResourceCursorAdapter` drop drown view resource.
                    resourceCursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
                    
                    // Get a handle for the `Spinner`.
                    Spinner spinner = (Spinner) findViewById(R.id.spinner);
                    
                    // Set the adapter for the folder `Spinner`.
                    spinner.setAdapter(resourceCursorAdapter);
                    

                    因为ResourceCursorAdapter在打开和关闭时使用相同的bindView填充微调器,所以spinner_dropdown_item.xmlspinner_item.xmlTextView的id必须相同。

                    spinner_item.xmllayout 文件夹中:

                    <?xml version="1.0" encoding="utf-8"?>
                    
                    <TextView
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/spinner_item_textview"
                        android:layout_height="wrap_content"
                        android:layout_width="match_parent"
                        android:maxLines="1"
                        android:ellipsize="end"
                        android:paddingStart="10dp"
                        android:paddingEnd="10dp"
                        android:textSize="18sp"
                        android:textColor="@color/primaryTextColor" />
                    

                    【讨论】:

                      猜你喜欢
                      • 2011-08-15
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-03-15
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多