【问题标题】:how do you set the spinner text color?你如何设置微调器文本颜色?
【发布时间】:2012-03-25 13:20:30
【问题描述】:

我无法在微调器小部件上设置颜色。这是如何设计的?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    尝试将此适配器用于您的微调器:

    ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(Home.Home_Group, R.layout.my_spinner_style, yourstringarray)
    {
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
    
            ((TextView) v).setTextSize(16);
            ((TextView) v).setTextColor(
                getResources().getColorStateList(R.color.white)
            );
    
            return v;
        }
    
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View v = super.getDropDownView(position, convertView, parent);
            v.setBackgroundResource(R.drawable.spinner_bg);
    
            ((TextView) v).setTextColor(
                getResources().getColorStateList(R.color.spinner_text)
            );
    
            ((TextView) v).setTypeface(fontStyle);
            ((TextView) v).setGravity(Gravity.CENTER);
    
            return v;
        }
    };
    

    将此 xml 添加到您的布局中,

    my_spinner_style.xml

    <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+android:id/text1"
            style="?android:attr/spinnerItemStyle"
            android:singleLine="true"
            android:textColor="#ffffff"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee" />
    

    最后,

    spinner.setAdapter(adapter);
    

    【讨论】:

      【解决方案2】:

      简单明快....

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

      【讨论】:

      • 正是我所需要的。但是,如果您在 Activity 的 onCreate() 函数中执行此操作,请使用 your_spinner.setOnItemSelectedListener(),不要忘记在您的移动设备的方向使用 因为否则你会得到一个空错误。
      【解决方案3】:

      最简单的形式是:

      m_spnDia = (Spinner)findViewById(R.id.spiDia);
      TextView oTextView = (TextView)m_spnDia.getChildAt(0);
      oTextView.setTextColor(Color.RED);
      

      【讨论】:

        【解决方案4】:

        如果你想改变文字颜色: Spinner Widgets Text Color

        否则,按照 JoxTraex 所说,制作自己的布局是最好的方式。

        【讨论】:

          【解决方案5】:

          Andro'd 答案的一个更简短的替代方法是让 ArrayAdapter 从布局资源为您创建项目视图:

          final List<String> values = [SPINNER VALUES];
          final ArrayAdapter<String> adapter = new ArrayAdapter<>(
              activity, R.layout.my_spinner_item, values);
          adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
          spinner.setAdapter(adapter);
          

          然后在my_spinner_item.xml 中设置适合您需要的文本样式:

          <?xml version="1.0" encoding="utf-8"?>
          <TextView
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@android:id/text1"
              style="@style/my_spinner_item_style"
          />
          

          注意:出现选项列表时使用my_spinner_dropdown_item

          有关更多信息,请阅读Spinners 文档。

          【讨论】:

            【解决方案6】:

            尝试了解您使用的是 SDK 中提供的默认值提供的下拉列表。

            简单使用自定义适配器制作您自己的布局。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-08-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多