【问题标题】:Set text color in Android Spinner在 Android Spinner 中设置文本颜色
【发布时间】:2017-09-22 22:16:40
【问题描述】:

我想在 Android 中更改我的微调器中显示的选定项目的颜色(针对 API 级别 16 及更高级别)。我已经尝试过在 SO 上发布的几种解决方案,包括为我的微调器项目创建自定义布局并使用 ColorStateList 作为自定义布局的文本颜色属性,但无济于事。微调器显示在半透明背景上 - 因此项目的自定义布局不起作用,因为它为微调器添加了颜色。目前我的黑客解决方案是

if (_colorCodeSpinner.getSelectedView() != null) {
    ((TextView) _colorCodeSpinner.getSelectedView()).setTextColor(0xFFFFFFFF);
}

但这仅在所选视图不为空(即方向更改时)时才有效。

我不敢相信没有设置文本颜色的简单解决方案。这似乎是你经常做的事情。与更改箭头颜色相同,我目前通过

_colorCodeSpinner.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

我错过了什么吗?更改微调器颜色的推荐方法是什么?

如图所示,在微调器中显示的选定项的文本颜色为黑色,但我想将其更改为白色。

编辑

澄清一下:我不是在寻找在运行时覆盖值的一小段代码(比如我在这个问题中发布的两个 sn-ps)。我正在寻找一种正确执行此操作的实际方法(例如在 XML 布局中或通过主题)。设置一次文本颜色属性,这样我就不必每次都更新它,例如选择一个项目。

【问题讨论】:

    标签: android android-layout spinner android-spinner


    【解决方案1】:

    这样做:

      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE); /* if you want your item to be white */
          }
    
          @Override
          public void onNothingSelected(AdapterView<?> parent) {
          }
      });
    

    【讨论】:

    • 这太棒了,它对我的​​击球手有用,非常感谢@HugoHouyez
    【解决方案2】:

    你可以实现这个编辑styles.xml布局文件。对于这个答案,我在 Android Studio 中使用了一个新项目,带有 minSdkVersion 16 和 AppCompatSpinner。

    styles.xml 布局:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    
        <item name="android:spinnerItemStyle">@style/mySpinnerItemSelectedStyle</item>
    </style>
    
    <style name="mySpinnerItemSelectedStyle" parent="@android:style/Widget.Holo.TextView.SpinnerItem">
        <item name="android:textColor">@color/spinnerTextColor</item>
    </style>
    

    并将其添加到 colors.xml 文件中:

    <color name="spinnerTextColor">#ffffff</color>
    

    解决方案取自以下链接。虽然它用于颜色微调器下拉项目,但大多是相同的方法。

    https://stackoverflow.com/a/22207394/6514926

    【讨论】:

      【解决方案3】:

      这对你有用

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


      你可以使用选择器来改变颜色

      创建一个名为 my_selctor.xml 的 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="black" /> <!-- pressed -->
           <item android:state_focused="true"
                 android:color="black" /> <!-- focused -->
           <item android:color="white" /> <!-- default -->
       </selector>
      

      在你的文本视图中这样设置

      <TextView ...........
         android:textColor=""@drawable/my_selctor"/>
      

      【讨论】:

        【解决方案4】:

        试试下面的代码:-

        XML:-

           <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_margin="20dp"
                android:popupBackground="#ffffff"
                android:layout_height="match_parent">
        
            </Spinner>
        

        为文本视图创建另一个 xml

        <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="dshsgv"
        android:padding="5dp"
        android:textColor="#000000">
        
        </TextView>
        

        然后在你的活动中:-

        public class MainActivity extends AppCompatActivity {
        Spinner spinner;
        String[] cat = {"Automobile", "Automobile"};
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            spinner = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<String> adpter = new ArrayAdapter<String>     (MainActivity.this, R.layout.text, cat);
            spinner.setAdapter(adpter);
         }
         }
        

        【讨论】:

          【解决方案5】:

          像这样声明 ArrayAdapter 并将其设置为您的微调器:

          ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
                                      R.layout.simple_spinner_dropdown_item, your_strings);
          adapter_state.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
          _colorCodeSpinner.setAdapter(adapter_state);
          

          和布局xml文件simple_spinner_dropdown_item.xml:

          <?xml version="1.0" encoding="utf-8"?>
          <TextView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="20sp"
              android:gravity="left"
              android:textColor="#AAA"
              android:padding="5dp"
              />
          

          这对我有用

          【讨论】:

            【解决方案6】:

            关注link

            private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            
                   ((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
                   ((TextView) parent.getChildAt(0)).setTextSize(12);
            
                }
            
                public void onNothingSelected(AdapterView<?> parent) {
            
                }
            };
            

            【讨论】:

              【解决方案7】:

              你可以这样使用。这将更改您的DropDown 菜单图标。

              spinner.getBackground().setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
              

              然后像这样用spinner_text.xml 制作一个TextView 布局名称

              <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/spinnerText"
              style="?android:attr/spinnerDropDownItemStyle"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:ellipsize="marquee"
              android:textColor="#fff" />
              

              并在您的 MainActivity.java 类中编写此代码

               List<String> categories = new ArrayList<String>();
                  categories.add("Automobile");
                  categories.add("Business Services");
                  categories.add("Computers");
                  categories.add("Education");
                  categories.add("Personal");
                  categories.add("Travel");
              
                  ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, categories);
                  spinner.setAdapter(adapter);
                  spinner.getBackground().setColorFilter(ContextCompat.getColor(this,R.color.white), PorterDuff.Mode.SRC_ATOP);
                  // attaching data adapter to spinner
                  spinner.setAdapter(adapter); 
              

              【讨论】:

              • 它改变微调器背景颜色而不是文本颜色
              猜你喜欢
              • 1970-01-01
              • 2012-04-06
              • 2012-09-13
              • 2016-08-07
              • 1970-01-01
              • 2023-03-11
              • 1970-01-01
              • 1970-01-01
              • 2012-09-13
              相关资源
              最近更新 更多