Java 如何在android中更改Listview项目颜色和textcolor

Java 如何在android中更改Listview项目颜色和textcolor,java,android,android-layout,listview,android-studio,Java,Android,Android Layout,Listview,Android Studio,在我的应用程序中,我创建了一个Listview。单击该行时,我希望将Listview行的颜色更改为浅灰色,将textcolor更改为蓝色 为此,我尝试了下面的代码,但只有行背景颜色在改变,而不是文本颜色 mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<

在我的应用程序中,我创建了一个Listview。单击该行时,我希望将Listview行的颜色更改为浅灰色,将textcolor更改为蓝色

为此,我尝试了下面的代码,但只有行背景颜色在改变,而不是文本颜色

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int j = 0; j < parent.getChildCount(); j++) {

                    parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
                    view.setBackgroundColor(Color.LTGRAY);

                    rowText = (TextView) findViewById(R.id.rowitem);
                    rowText.setTextColor(Color.BLUE);
                }


   }
    });
}
mainListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
对于(int j=0;j
首先创建一个可绘制的选择器,如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/red"
          android:state_pressed="true" />
    <item android:drawable="@android:color/white" />
</selector>

改变

 rowText = (TextView) findViewById(R.id.rowitem);


因为您应该在膨胀视图中找到textview,而不是单独找到它。

首先在ListView中启用android:ListSelector属性

ListView android:id="@+id/android:list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Tablayoutdesign"
    android:cacheColorHint="#000000"
    android:dividerHeight="1dip"
    android:layout_marginTop="63dip"
    android:layout_marginBottom="40dip"

    />
感谢创建选择器(listselector.xml)


而不是使用colors.xml

<resources>
<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>

#ff5500
#FF00FF
还有一些Java魔法: listview.setSelector(R.drawable.listselector)


感谢@sankar anesh

按下列表状态

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

未按时列表查看背景

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#4285f4" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:bottom="2dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFF" />
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

列表视图选择器

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/card_state_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/card_background" />
</selector>


当我点击假设第一行,然后第一行背景和文本颜色必须改变,当我点击第二行,然后第二行背景和文本颜色必须改变,并且第一行背景和文本颜色必须位于前面的colorsOk,然后用你刚才告诉我的内容更新你的问题,同时我也在更新答案
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/card_state_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/card_background" />
</selector>