我有一个ListView,它在手机上效果很好。现在我正在制作平板电脑用户界面,左边是ListView,右边是详细信息。Android:ListView选择后保持蓝色背景
当我触摸一个项目时,只要按下它就会闪烁蓝色。我想保持蓝色,直到选中另一个项目,就像Nexus 7上的Gmail应用程序一样。
实现该目标的最简洁方法是什么?我宁愿避免手动设置背景,我认为有一种方法可以将元素标记为“活动”元素,然后相应地设置主题。
我有一个ListView,它在手机上效果很好。现在我正在制作平板电脑用户界面,左边是ListView,右边是详细信息。Android:ListView选择后保持蓝色背景
当我触摸一个项目时,只要按下它就会闪烁蓝色。我想保持蓝色,直到选中另一个项目,就像Nexus 7上的Gmail应用程序一样。
实现该目标的最简洁方法是什么?我宁愿避免手动设置背景,我认为有一种方法可以将元素标记为“活动”元素,然后相应地设置主题。
什么是最简单的方法来实现这一目标?
你在找什么是被称为“激活”状态。要做到这一点:
步骤#1:在res/values-v11/
,有一个样式资源,执行activated
。例如,对于已定义那里AppTheme
声明一个新的项目,去的东西,如:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
第2步:定义同风格res/values/
任何较旧的设备,就像一个存根风格的资源,所以引用它继续工作:
<resources>
<style name="AppTheme" parent="android:Theme.Light"/>
<style name="activated" parent="AppTheme"/>
</resources>
第3步:在在ListView
行布局XML资源,增加style="@style/activated"
根元素
圣属性列表EP#4:设置ListView
是单选项列表,如在ListFragment
以下行:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
可以在this sample project,this sample project和this sample project看到这个动作。对于那些第一次两个样本更多的背景,请参阅本SO问题:Complete Working Sample of the Gmail Three-Fragment Animation Scenario?
它工作得很好,它确实比以编程方式更改背景好得多。非常感谢你! – erwan
使用的
android.R.layout.simple_list_item_activated_1
代替
R.layout.simple_list_item_checkable_1
。
只为有人检查某一天。
经过几天的搜索和拉我的头发我刚刚发现,activatedBackgroundIndicator
也可在ActionBarSherlock造型系统。大多数开发向后兼容应用程序的开发人员使用ActionBarSherlock,因此在大多数情况下使用ActionBarSherlock是一个不错的选择。因此,而不是使用android:background="?android:attr/activatedBackgroundIndicator"
将前11给android的版本错误,只需使用:android:background="?activatedBackgroundIndicator"
这里是行的例子布局XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textSize="15sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="5dip"
android:textSize="20dip" />
</LinearLayout>
'所以,而不是使用android:background =“?activatedBackgroundIndicator”,它会在11之前的Android版本中发生错误,只需使用:android:background =“?activatedBackgroundIndicator”'什么?你能解释一下吗? – kuchi
谢谢4点指出。纠正了答案。 –
你可以看到这个链接可能会有所帮助:HTTP:/ /stackoverflow.com/questions/5682053/listview-item-wont-stay-selected –
你可以设置列表视图项目的点击ListView项目的视图的背景颜色。 –