[英]Is there a way to change the text color of an item in a ListView when it is selected?
I'm working with Android Studio. 我正在使用Android Studio。
I have an activity that creates a list of items as follows: 我有一个活动,它创建一个项目列表,如下所示:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_listado_categorias); inicializarControles(); .... }
Where: 哪里:
private void inicializarControles() { ... m_itemCategoria = new ArrayAdapter<>(this, R.layout.act_item_listview); m_lvwCategorias = (ListView) findViewById(R.id.lvwCategorias); m_lvwCategorias.setAdapter(m_itemCategoria); m_lvwCategorias.setOnItemClickListener(onItemCategoriaListViewOnClickListener); m_lvwCategorias.setOnItemLongClickListener(onItemCategoriaListViewOnItemLongClickListener); m_lvwCategorias.setOnDragListener(onItemCategoriaListViewOnDragListener); .... }
The onDragListener event is currently running successfully. onDragListener事件当前正在成功运行。
My question is: 我的问题是:
Is there a way that when I select an item from the list it can change the color of the text? 当我从列表中选择一个项目时,是否可以更改文本的颜色? (Only selected item)
(仅所选项目)
Thanks 谢谢
You need to set your child views to: 您需要将子视图设置为:
android:duplicateParentState="true"
Now you can use the methods to declare your TextViews' colors using a selector such as: 现在,您可以使用方法通过选择器声明TextViews的颜色,例如:
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>
As you can see, @color values are allowed. 如您所见,允许@color值。 Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.
另外-android:state_pressed与AdapterView.OnItemClickListener结合使用。 Hope this helps.
希望这可以帮助。
or you can try this, 或者你可以尝试一下
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView <? > adapterView, View rowView,
int position, long id) {
TextView textView = (TextView) rowView.findViewById(R.id.rowListTextView);
textView.setTextColor("Desired Color");
}
}); });
Yes. 是。 By using a color selector.
通过使用颜色选择器。
Under your res
folder in you app project create a directory named color
. 在应用程序项目的
res
文件夹下,创建一个名为color
的目录。 In this color directory add a XML file (eg., color_selector.xml). 在此颜色目录中添加XML文件(例如color_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="@color/COLOR_WHEN_PRESSED" />
<item android:color="@color/COLOR_NORMALLY" />
</selector>
Your color_selector.xml should look like this. 您的color_selector.xml应该如下所示。
Then in your xml file where you declared your view that contains text. 然后在xml文件中声明包含文本的视图。 Set the android:textColor attribute to this selector.
将android:textColor属性设置为此选择器。 Like so:
像这样:
android:textColor="@color/color_selector"
You may also declare textColor programmatically by: 您还可以通过以下方式以编程方式声明textColor:
TextView text;
text.setTextColor(getResources().getColorStateList(R.color.choose_alarm_color_selector));
Create a Color Selector file in your drawable folder, let's say you name it "text_selector.xml", then paste this code in that file: 在可绘制文件夹中创建一个颜色选择器文件,假设您将其命名为“ text_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="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
Now you just simply have to set the set the color of your textview in your "act_item_listview.xml" file with this selector file like this: 现在,您只需使用以下选择器文件在“ act_item_listview.xml”文件中设置文本视图的颜色即可:
Define a property in your textview object: 在您的textview对象中定义一个属性:
android:textColor="@drawable/text_selector"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.