2016-03-08 145 views
0

在我的应用程序中,我创建了一个Listview。当我点击该行时,我想将Listview行颜色更改为Lightgray颜色和textcolor蓝色。如何在Android中更改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); 
       } 


    } 
    }); 
} 

回答

0

首先创建一个选择绘制这样的:

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

然后

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

更改

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

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

因为您应该在充气视图中找到textview,而不是独立。

+0

当我点击时假设第一行然后第一行背景和textcolor必须改变,当我点击第二行然后第二行背景和textcolor必须改变并且第一行背景和textcolor必须来以前的颜色 – AbhiRam

+0

好的,然后用你告诉我的内容更新你的问题,同时我正在更新答案 –

+0

好的请更新 – AbhiRam

0

首先启用了android:ListSelector属性在你的ListView

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)

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

比去colors.xml

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

和一些Java魔术: listview.setSelector(R.drawable.listselector)

感谢@桑卡尔 - anesh

0

列表状态下按下

<?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> 
相关问题