wenjun12369 2012-01-05 22:43
浏览 273
已采纳

listview 获得焦点 item背景图片变化

[b][/b]我需要的是item选中时,item的背景图片切换。但是实现不了。

这是listview xml

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

android:id="@+id/list_id"
android:layout_weight="1"
android:paddingTop="40dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:listSelector="@drawable/list_picture"
android:focusable="true"
/>

内部条目的xml

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/item3" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#545454"
android:textSize="25px"
android:textStyle="bold"
android:id="@+id/first"/>

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#545454"
android:id="@+id/second"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:id="@+id/third"/>

切换图片的xml

<?xml version="1.0" encoding="utf-8" ?>

android:state_window_focused="false"
android:drawable="@drawable/item3" />
android:state_pressed="true"
android:drawable="@drawable/itemchange" />
android:state_pressed="true"
android:drawable="@drawable/itemchange" />
android:drawable="@drawable/itemchange" />
android:drawable="@drawable/itemchange" />

在Activity中如果是
[b]listview= new listview(this);就不会报错 但这样不能获取listview 的id;
listview=(listview)findViewById(R.id.list_id);就会报错,nullpointerexception;[/b]

开始时没有考虑item换背景的问题,所以没有listview xml文件,现在想把橙黄色背景换成图片。却出现错误。

在没有继承listActivity时,取id报错,要是继承listActivity时,item全部不见了,只剩下addfooter的添加内容。

求解释·········求解决

  • 写回答

3条回答

  • laopeng301 2012-01-06 10:21
    关注

    [code="java"] android:drawable="@drawable/没有焦点时的图片背景" />

    你检查下 你的这个图片。
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试