如何改变在Android的数组元素的颜色? [英] How to change the color of an array element in android?

查看:397
本文介绍了如何改变在Android的数组元素的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

这篇关于如何改变在Android的数组元素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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