【发布时间】:2013-03-11 17:02:07
【问题描述】:
我正在使用 ActionBarSherlock,并且正在尝试为行背景自定义 activatedBackgroundIndicator 属性。
如果我使用最新的 android sdk,没有 ActionBarSherlock,我可以自定义背景,在 res/values/style.xml 上创建以下样式和在 AndroidManifest.xml 上将其定义为 android:theme="@style/Theme.Custom":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Custom" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
</resources>
然后,我的 res/drawable/activated_background.xml 包含下一个代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/row_activated" />
<item android:drawable="@android:color/transparent" />
</selector>
最后,用于定义ListView中每一行的代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator">
<TextView
android:id="@+id/test_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sample_string"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="15dp"/>
</LinearLayout>
结果显示在屏幕截图中。是一个简单的应用程序,在单击列表项时只有一个 ListView 和 ListView.CHOICE_MODE_SINGLE 和 getListView().setItemChecked(position, true)。
标准选定行的蓝色现在是黄色并且可以完美运行。
当我想使用 ActionBarSherlock 应用相同的自定义时,就会出现问题。 现在样式文件是下一个,背景显示为蓝色而不是自定义的黄色。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Custom" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="activatedBackgroundIndicator">@drawable/activated_background</item>
<item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
</style>
<style name="activatedBackgroundIndicator">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
我不知道 ActionBarSherlock 是否支持 android:activatedBackgroundIndicator 功能,或者我是否忘记实现一些需要能够更改默认颜色的东西。
有什么想法吗?
【问题讨论】:
-
ActionBarSherlock 确实支持activatedBackgroundIndicator 属性。另外,为什么你的 XML 中定义了一个名为“activatedBackgroundIndicator”的样式?
-
我搜索了实现此功能的项目,发现Put.io 正在使用它。
-
我添加了样式以使其正常工作,但如果没有必要,我将删除它。那么@Alex-Fu,使用ActionBarSherlock的实现必须和默认一样?
-
除非您将
activatedBackgroundIndicator称为样式,否则您不需要样式定义。Theme.Custom定义中的所有内容都是正确的。如果在 LinearLayout 中将android:background="?android:attr/activatedBackgroundIndicator"更改为android:background="?attr/activatedBackgroundIndicator"会发生什么? -
此外,您应该在可绘制选择器中包含尽可能多的状态。
android:state_activated="true"不足以涵盖所有可能性,例如长按、点击、聚焦等...
标签: android android-layout background actionbarsherlock android-theme