Android:选择ListView后保持蓝色背景
我有一个ListView,它在手机上工作得很好。现在我正在制作一个tablet UI,左边是ListView,右边是details 当我触摸一个项目时,只要按下它,它就会闪烁蓝色。我想保持蓝色直到另一个项目被选中,就像Nexus7上的Gmail应用一样 实现这一目标最干净的方法是什么?我宁愿避免手动设置背景,我假设有一种方法可以将一个元素标记为“活动”元素并相应地设置主题 实现这一目标最干净的方法是什么 您正在寻找的是所谓的“激活”状态。要实现这一目标: 步骤1:在Android:选择ListView后保持蓝色背景,android,listview,tablet,Android,Listview,Tablet,我有一个ListView,它在手机上工作得很好。现在我正在制作一个tablet UI,左边是ListView,右边是details 当我触摸一个项目时,只要按下它,它就会闪烁蓝色。我想保持蓝色直到另一个项目被选中,就像Nexus7上的Gmail应用一样 实现这一目标最干净的方法是什么?我宁愿避免手动设置背景,我假设有一种方法可以将一个元素标记为“活动”元素并相应地设置主题 实现这一目标最干净的方法是什么 您正在寻找的是所谓的“激活”状态。要实现这一目标: 步骤1:在res/values-v11/
res/values-v11/
中,激活实现的样式资源。例如,对于定义了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>
步骤3:在列表视图
行的布局XML资源中,将style=“@style/activated”
添加到根元素的属性列表中
步骤4:将列表视图
设置为单选列表,如列表片段
中的以下行:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
你可以在和中看到这一点。有关前两个示例的更多背景信息,请参见此SO问题:使用
android.R.layout.simple\u list\u item\u activated\u 1
而不是
R.layout.simple\u list\u item\u checkable\u 1
只是为了某一天的检查。经过几天的搜索和梳理,我才发现,激活的背景指示器
也可以在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>
您可以看到此链接可能会有所帮助:您可以在单击listview项时设置listview项视图的背景色。它非常有效,而且确实比以编程方式不断更改背景要好得多。多谢各位<代码>因此,不要使用android:background=“?activatedBackgroundIndicator”,它会在11之前的android版本中显示错误,只需使用:android:background=“?activatedBackgroundIndicator”
什么?你能解释一下吗?
<?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>