关于android:设置ListView的选中状态

1次阅读

共计 1490 个字符,预计需要花费 4 分钟才能阅读完成。

设置 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,这也是在做的途中遇到的细小问题,拿进去共勉。有什么不对的中央还请斧正

正文完
 0