设置ListView的选中状态:android:state_activated="true"
图片
Developer发布于 2016-06-04
图片
1.说明看这篇文章之前,首先你得知道怎么通过Adapter使用ListView控件,不然请不要往下看。主要是为了便于快速阅读,删除了很多多余的代码。包含了字体颜色和背景颜色的改变,纯xml。没有上源代码,有时间再进行整理。2.演示
图片
3.步骤:1.ListView控件中必须设置属性,不然是不起作用的:android:choiceMode="singleChoice"2.要想让ListView中的文字在激活状态下变为白色,需要在drawable文件夹下设置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="@android:color/white" />
   <!-- 默认为黑色 -->
   <item
       android:color="#000" />

</selector>
3.然后在子项布局文件的TextView中进行引用这个xml文件android:textColor="@drawable/menu_item_text_color"就可以了: <TextView
android:textColor="@drawable/menu_item_text_color"
android:text="首页"
/>4.设置背景颜色(同上面一样):xml布局(这里的drawable属性的值不能是#222这种的,必须定义在资源文件中,不然会报错): <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item
       android:state_activated="true"
       android:drawable="@color/grey" />
   <item android:drawable="@android:color/transparent" />

</selector>5.在LinearLayout中设置属性: android:background="@drawable/menu_item_background" <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:background="@drawable/menu_item_background">
   <TextView
       android:textColor="@drawable/menu_item_text_color"
       android:text="首页"
       />

</LinearLayout>
4.注意:别忘了设置ListView的属性android:choiceMode="singleChoice",不然是看不出android:state_activated="true"的作用的。5.最近一直在学着模仿知乎APP,这也是在做的途中遇到的细小问题,拿出来共勉。有什么不对的地方还请指正


路漫出行
1 声望0 粉丝