如何改变在Android的数组元素的颜色?数组、元素、颜色、Android

2023-09-03 23:22:59 作者:△汐夏*

我只是想知道,我们可以改变一个特定的数组索引的颜色?我有一个数组如下: -

I just want to know , can we change the color of a particular array index? i have a following array-:

 String [] all={"1","2","3","4","5","6","7","8","9","10"};

所以,我想打印在红色数字6,并通过ArrayAdapter休息,在黑色的。

So , i want to print number 6 in red color and rest in black through ArrayAdapter.

我怎样才能改变数组索引的颜色?请帮助我!

How can i change the color of the array index? Please help me out !!!

推荐答案

文字颜色创建下面的XML在绘制文件夹:

for text color create following xml in drawable folder:

item_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_activated="true" android:color="#777"/>
    <item android:state_focused="true" android:color="#000"/>
    <item android:color="#000"/>

</selector>

现在用的TextView创建布局只:

Now create a layout with textview only:

item_layout.xml

<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:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="@drawable/item_bg"
     />

在code:

new ArrayAdapter<String>(this, R.layout.item_layout);

现在,如果你想第6项有不同的文字颜色通话

now if you want 6th item to have different text color call

mylistview.setChoiceMode(1);
mylistview.setItemChecked(6, true);