【问题标题】:Change textcolors of Android ListView text on selection在选择时更改 Android ListView 文本的 textcolors
【发布时间】:2012-01-24 12:27:29
【问题描述】:

我不知道我做错了什么......

我有一个带有自定义 layout.xml 文件的 ListView。在那里,我定义了一个这样的 TextView

<TextView android:layout_height="wrap_content" 
          android:layout_width="wrap_content"
          android:text="foo"
          android:textColor="@drawable/listitem_textcolor_selector"/>

listitem_textcolor_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="#ff0000" />
    <item android:state_selected="true" android:color="#ff0000" />
    <item android:state_focused="true" android:color="#ff0000" />
    <item android:color="#000000" />
</selector>

这种作品。如果我选择一行,它将正确地将文本的颜色更改为红色。唯一的问题是,它不会保持红色。 大约一秒钟后,该颜色将变回黑色。

这里的主要问题是行的背景会改变它的颜色并且这种颜色会保持不变,但文本的颜色不会,即使列表项的选择器本身看起来相同(期望颜色)。

谁能告诉我我错过了什么? 感谢任何帮助,因为我不知道如何解决这个问题:)

谢谢

编辑: 也许我还应该指出,我正在三星 Galaxy Tab 10.1 平板电脑上进行测试。我曾经在不知道天气的情况下听说过有关“TouchMode”的内容,这与我的问题有关...

【问题讨论】:

    标签: android listview user-interface


    【解决方案1】:

    在 listitem_textcolor_selector.xml 添加这些状态:

            <!-- Activated -->      
    <item 
        android:state_activated="true" 
        android:color="#ff0000" />
    
            <!-- Active -->      
    <item 
        android:state_active="true" 
        android:color="#ff0000" />  
    

    在此之后,所选项目将保持其颜色状态,直到选择其他内容。

    希望这会有所帮助。

    【讨论】:

    • 谢谢 Lonut,经过一个小时的绝望找到了您的解决方案
    【解决方案2】:

    我赢了这个问题! 在选择器中:

        <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">  
        <item  android:state_activated="true"  android:color="#ff0000" />
        <item android:state_active="true" android:color="#ff0000" />  
        <item android:state_pressed="true" android:color="#ff0000" />
        <item android:state_selected="true" android:color="#ff0000" />
        <item android:state_focused="true" android:color="#ff0000" />
        <item android:color="#000000" />
    </selector>
    

    活动中:myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    public void onItemClick(AdapterView<?> parent, View view,
                          int position, long id) {
                         pl=position;
                         myListView.setItemChecked(pl, true);
                         adapter.notifyDataSetChanged();
                              .....
    

    item.xml:

    <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:textColor="@drawable/selector"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        />
    

    在我点击它们后,我选中的项目是红色的。只有一项检查项目。尽情享受吧!

    【讨论】:

      【解决方案3】:

      我知道了。如果您为列表项使用自定义 xml,则可以更改文本颜色,例如:

      custom_listitem:

      <?xml version="1.0" encoding="utf-8"?>     
          <TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingLeft="10dip"
              android:paddingRight="10dip"
              android:paddingTop="7dip"
              android:paddingBottom="7dip"
              android:textSize="18sp"
              android:textStyle="bold"
              android:textColor="@drawable/listitem_textcolor_selector"
               />
      

      现在您必须将此 xml 名称用于您用来填充列表视图的数组适配器。

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_listitem, options);
      listView.setAdapter(adapter);
      

      【讨论】:

      • 我的主要问题是,不仅有一个 TextView ...在“选定”状态下都具有相同的颜色。无论如何,我将 R.layout.list_entry.xml 文件传递​​给我的自定义子类 ArrayAdapter...
      • 那么你需要有不同的 textcolor_selector 文件,并根据你的所有 textview 或任何你需要改变颜色的地方进行设置。
      • 所以基本上,在不同的 text_selector 文件中,您只需要更改正常状态的颜色..没有其他选项可以满足您的要求。
      • 我确实有几个颜色选择器文件......像这样: 你是这个意思吗?因为那样它不起作用:(
      • 不...不是那样。我说的是-当您准备listitem_textcolor_selector时,准备其他具有相同代码的文件,但根据您想要的正常文本颜色更改每个文件的正常状态颜色xml 中的不同组件。
      【解决方案4】:

      listitem_textcolor_selector.xml 保存在color folder of your res 中,然后像这样使用它:android:textColor="@color/listitem_textcolor_selector"

      【讨论】:

      • 感谢您将其保存在更合适的文件夹中的提示:) 但不幸的是,这并没有成功... :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2015-02-01
      相关资源
      最近更新 更多