How to change the color of an array element in and

2020-05-01 08:38发布

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"};

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 !!!

2条回答
霸刀☆藐视天下
2楼-- · 2020-05-01 08:51

Yes you can do it simply...

for (int i = 0; i < all.length; i++) {
        if(i<=6){
            Textview.setTextColor(Color.RED);
        }else{
            Textview.setTextColor(Color.BLACK);
        }
    }

if you need any kind of help let me know!!

查看更多
\"骚年 ilove
3楼-- · 2020-05-01 08:58

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>

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"
     />

in code:

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

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

mylistview.setChoiceMode(1);
mylistview.setItemChecked(6, true);
查看更多
登录 后发表回答