【问题标题】:Changing color of text in TextView which is rendered using ArrayAdapter in android activity更改在 Android 活动中使用 ArrayAdapter 呈现的 TextView 中的文本颜色
【发布时间】:2014-12-22 18:18:46
【问题描述】:

我在选择器标签内使用以下 xml 标签:

        <item android:state_enabled="true" android:color="@color/red"/>
        <item android:color="@color/black"/>

我使用这个选择器来指定 TextView 的 textColor(文件名为 = country_code_spinner_item)。

此 TextView 在以下 ArrayAdapter 中使用:

        ArrayList<String> countrySpinnerOptions = new ArrayList<>(
                        CountryData.getInstance().getSortedCountryCodes());
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        R.layout.country_code_spinner_item,
                        countrySpinnerOptions)

This adapter is added to a **Spinner Object**.

Inside the file named = **country_code_spinner_item**
    I have specified :   **android:enabled="false"**
    and     **android:textColor="@color/country_code_selector"**

Here country_code_selector is the file in which i have specified :
        **<item android:state_enabled="true" android:color="@color/red"/>**

现在,当这个 TextView 将被渲染时,启用的标签被设置为 false 硬编码。 TextView 的 textColor 应设置为黑色。但是我看到 textColor 是红色的,尽管我已经指定了 android:enabled = false ,然后应该在选择器中选择第二个选项。

我基本上希望将 TextView 中的文本颜色默认保持为黑色,并且当 TextView 被点击时(这是一个微调器对象),我希望将颜色永久更改为红色。 如何使用

对我来说似乎并不直接

感谢您的帮助

【问题讨论】:

    标签: android


    【解决方案1】:

    在 ArrayAdapter 上,默认启用所有项目(除非适配器从 isEnabled 方法返回 false),因为 areAllItemsEnabled 方法在 BaseAdapter 中返回 true。

    您可以尝试改用android:state_checked 状态,或者创建一个自定义适配器,根据当前选择的位置在 TextView 上设置启用真/假。

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      spinner_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:textColor="#000"
          android:text="Text View"
          android:id="@+id/spinnerText"
          android:layout_height="60dp">
      
      </TextView>
      

      在您填充微调器的活动中尝试

      your_spinner_object.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                  TextView tv = (TextView)view.findViewById(R.id.spinnerText);
                  tv.setTextColor(Color.RED);
              }
      
              @Override
              public void onNothingSelected(AdapterView<?> parent) {
      
              }
          });
      

      希望对你有帮助!!!

      【讨论】:

        猜你喜欢
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多