如何更改选定/聚焦的 ListView 项目中的字体颜色? [英] how to change font color in selected/focused ListView items?

查看:23
本文介绍了如何更改选定/聚焦的 ListView 项目中的字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此苦苦挣扎,这显然是一个非常简单的效果,但令人难以置信的是,还没有找到在 Android 中执行此操作的任何直观方法.

I am struggling with this which apparently is a very simple effect but incredibly haven't found any intutitive way for doing it in Android.

我有一个 ListView 并且我设法自定义背景图像,以便通过获取新的可绘制背景突出显示所选项目.这我创建了一个新样式,我将 android:listSelector 属性设置为指向一个 StateListDrawable,其中我指定了用于每个状态的可绘制对象.

I have a ListView and I managed to customize the background images so the selected item gets highlighted by getting a new background drawable. This I do creating a new style where I set the android:listSelector attribute to point a StateListDrawable where I have specified which drawables to use for every state.

但是,每个 ListView 项目都是一个 LinearLayout,其中我有两个 TextView.我的目标是能够在选择或按下父视图时更改这些子视图的文本颜色,同时与背景可绘制对象相同.我知道有一个 ColorStateList,但没有成功设置.

However each ListView item is a LinearLayout where i have two TextViews. My goal is to be able to change the text color of these child views whenever the parent is selected or pressed, at the same time as the background drawable does. I know there is a ColorStateList but haven't been succesful setting that up.

有没有人成功地让这样的东西工作?

Has anybody succeed getting something like this to work?

谢谢.

推荐答案

当您的 ListView 受到具有多个视图的布局的影响时,这些都不是可能的答案.您需要将您的子视图设置为:

Neither of these are possible answers when your ListView is compromised of a layout that has multiple views. You need to set your child views to:

android:duplicateParentState="true"

现在您可以使用其他人在上面描述的方法来使用选择器声明您的 TextViews 的颜色,例如:

Now you can use the methods others have described above to declare your TextViews' colors using a selector such as:

android:textColor="@drawable/my_row_selector"

我相信你知道,但是选择器可以很简单:

and I'm sure you're aware, but the selector can be as simple as:

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

如您所见,@color 值是允许的.希望这会有所帮助.

As you can see, @color values are allowed. Hope this helps.

此外 - android:state_pressed 与 AdapterView.OnItemClickListener 结合使用.

Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.

这篇关于如何更改选定/聚焦的 ListView 项目中的字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆