我在 android 中开发一个项目,其中包含一个 ListView 。我的目标是当我们触摸 ListView 的任何行或者当我们将鼠标指针放在任何列表项上时,它的背景颜色必须更改为灰色.这也必须在我们滚动时实现..任何人都可以帮助我解决它。
我的代码是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
private void populateListView() {
String[] str={"apple,orange,grape,strawberry,guava,pineapple,etc"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, R.layout.textview,str);
ListView list=(ListView)findViewById(R.id.listviewone);
list.setAdapter(adapter);
}
我的activity_main.xml
<ListView
android:id="@+id/listviewone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#19bee6"
android:listSelector="@drawable/listselect"
android:scrollbars="none">
</ListView>
列表选择.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@drawable/orange_color"/>
<item android:state_pressed="true" android:state_accelerated="true" android:drawable="@drawable/red_color"/>
<item android:state_hovered="true" android:drawable="@drawable/orange_color"/>
<item android:state_focused="true" android:drawable="@drawable/red_color"/>
<item android:state_active="true" android:drawable="@drawable/red_color"/>
<item android:drawable="@drawable/textgradient"/>
最佳答案
你可以试试这个:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black" android:state_enabled="false" android:state_focused="true"/>
<item android:drawable="@android:color/holo_red_dark" android:state_pressed="true"/>
<item android:drawable="@android:color/holo_red_dark" android:state_focused="true"/>
</selector>
您也可以查看this link :
关于android - 将鼠标悬停在 ListView 上时更改 ListView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522321/