android - ActionBarSherlock 上 activatedBackgroundIndicator 的自定义背景不起作用

标签 android android-layout background actionbarsherlock android-theme

我正在使用 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.CHOICE_MODE_SINGLEgetListView().setItemChecked(position, true) 的 ListView。

标准选定行的蓝色现在是黄色并且效果很好。

List without ABS

当我想使用 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>

List with ABS

我不知道 ActionBarSherlock 是否支持 android:activatedBackgroundIndicator 功能,或者我是否忘记实现能够更改默认颜色所需的东西。

有什么想法吗?

最佳答案

终于找到了解决问题的方法。
它是行适配器中使用的上下文。使用的构造函数如以下代码所示:

public RowAdapter(Activity activity, ArrayList<String> list) {
    this.mContext = activity.getApplicationContext();
    this.elements = list;
    this.theActivity = activity;

    this.inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

只需更改上下文:
this.mContext = activity.getApplicationContext()(应用程序上下文)

this.mContext = activity.getBaseContext()( Activity 上下文)
问题解决了,现在背景是自定义的。

Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.

answer帮助我理解为什么在我的案例中使用 getBaseContext() 而不是 getApplicationContext()

custom row background

关于android - ActionBarSherlock 上 activatedBackgroundIndicator 的自定义背景不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15344320/

相关文章:

java - 表达式 user-> { some code } 怎么可能是一个 Observer 对象呢?

android - 拥有外部调用的 secret URL 的替代方案

android - 如何在 Android 模拟器中检查 ldpi hdpi 图像

css - Opera CSS 问题 : Strange black backround in Opera 9. 63 和 10 beta 使用溢出和背景颜色的组合时

css - 主体宽度小于内部元素

android - Jacoco 无法读取 .ec 文件

java - 当我在 android 中取消选择甚至 1 个复选框时如何取消选择全选复选框

android - 在布局中插入首选项 XML

android - 在 Android 中向用户显示提示的新样式

java - JOGL透明背景