简体   繁体   English

Android:在ListView选择后保持蓝色背景

[英]Android: keep blue background after ListView selection

I have a ListView, and it works great on a phone. 我有一个ListView,它在手机上运行良好。 Now I am making a tablet UI, with the ListView on the left and details on the right. 现在我正在创建一个平板电脑用户界面,左边是ListView,右边有详细信息。

When I touch an item, it flashes blue as long as it is pressed. 当我触摸某个项目时,只要按下它就会闪烁蓝色。 I want to keep that blue color until another item is selected, just like the Gmail app on the Nexus 7. 我希望保持蓝色,直到选择了另一个项目,就像Nexus 7上的Gmail应用程序一样。

What is the cleanest way to achieve that? 实现这一目标的最简洁方法是什么? I'd rather avoid setting backgrounds manually, I assume there is a way to mark an element as the "active" one and theme it accordingly. 我宁愿避免手动设置背景,我认为有一种方法可以将元素标记为“活动”元素并相应地对其进行主题化。

What is the cleanest way to achieve that? 实现这一目标的最简洁方法是什么?

What you are looking for is known as the "activated" state. 您正在寻找的是被称为“激活”状态。 To make this work: 为了使这项工作:

Step #1: In res/values-v11/ , have a style resource that implements activated . 步骤#1:在res/values-v11/ ,有一个实现activated的样式资源。 For example, for a new project that has the AppTheme declaration defined there, go with something like: 例如,对于那里定义了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>

Step #2: Define the same style in res/values/ for any older devices, just as a stub style resource, so references to it continue to work: 步骤#2:在res/values/为任何旧设备定义相同的样式,就像存根样式资源一样,因此对它的引用继续有效:

<resources>

    <style name="AppTheme" parent="android:Theme.Light"/>

    <style name="activated" parent="AppTheme"/>

</resources>

Step #3: In your layout XML resource for the row in the ListView , add style="@style/activated" to the list of attributes of the root element 步骤3:在ListView的行的布局XML资源中,将style="@style/activated"到根元素的属性列表中

Step #4: Set the ListView to be a single-choice list, such as the following line in a ListFragment : 步骤#4:将ListView设置为单选列表,例如ListFragment的以下行:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

You can see this in action in this sample project , this sample project , and this sample project . 您可以在此示例项目此示例项目此示例项目中看到此操作。 For more background on those first two samples, see this SO question: Complete Working Sample of the Gmail Three-Fragment Animation Scenario? 有关前两个示例的更多背景信息,请参阅此SO问题: Gmail三片段动画方案的完整工作示例?

Using 运用

android.R.layout.simple_list_item_activated_1

instead of 代替

R.layout.simple_list_item_checkable_1 . R.layout.simple_list_item_checkable_1

Just for somebody checking someday. 只是有一天有人检查。

after days of search and pulling my hair i just found out that activatedBackgroundIndicator is also available in ActionBarSherlock styling system. 经过几天的搜索和拉扯我的头发,我发现在ActionBarSherlock样式系统中也可以使用activatedBackgroundIndicator Most of the devs which develop backwards compatible apps, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases. 开发向后兼容应用程序的大多数开发人员使用ActionBarSherlock,因此在大多数情况下使用ActionBarSherlock是一个不错的选择。 So instead of using android:background="?android:attr/activatedBackgroundIndicator" which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator" 因此,而不是使用android:background="?android:attr/activatedBackgroundIndicator" ,这将在11之前的Android版本中给出错误,只需使用: android:background="?activatedBackgroundIndicator"

here is the example row layout xml code: 这是示例行布局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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM