简体   繁体   English

如何在Android中更改ListView项目的颜色和文本颜色

[英]How to change Listview item color and textcolor in android

In my app I have created one Listview. 在我的应用中,我创建了一个Listview。 When I click on the row, I want to change the Listview row color as Lightgray color and textcolor as a Blue color. 当我单击该行时,我想将Listview行颜色更改为浅灰色,将textcolor更改为蓝色。

For this I have tried the code below but only row background color is changing not the textcolor. 为此,我尝试了下面的代码,但仅行背景色未更改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);
                }


   }
    });
}

First create a selector drawable like this : 首先创建一个选择器drawable,像这样:

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

Then 然后

StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
view.setBackgroundDrawable(selector);

Change the 改变

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

to

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

since you should find the textview in the inflated view, not independently. 因为您应该在展开视图中而不是独立地找到textview。

first enable the android:ListSelector attribute in your ListView 首先在您的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"

    />

Thank create a selector (listselector.xml) 感谢创建选择器(listselector.xml)

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

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 

</selector> 

Than go for colors.xml 比上colors.xml

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

and with some Java magic: listview.setSelector(R.drawable.listselector) 并带有一些Java魔术:listview.setSelector(R.drawable.listselector)

Thanks to @sankar-anesh 感谢@ sankar-anesh

List State Pressed 列出按下状态

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

List view background when not pressed 未按下时列出视图背景

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

List view Selector 列表视图选择器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM