ActionBarSherlock 上的activatedBackgroundIndicator 的自定义背景不起作用 [英] Custom background for activatedBackgroundIndicator on ActionBarSherlock doesn't work

查看:10
本文介绍了ActionBarSherlock 上的activatedBackgroundIndicator 的自定义背景不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ActionBarSherlock,并且正在尝试为行背景自定义 activatedBackgroundIndicator 属性.

I'm using ActionBarSherlock and I'm trying to customize the activatedBackgroundIndicator attribute for the row background.

如果我使用最新的 android sdk,没有 ActionBarSherlock,我可以自定义背景,在 res/values/style.xml 上创建以下样式和在 AndroidManifest.xml 上将其定义为 android:theme="@style/Theme.Custom":

If I use the latest android sdk, without the ActionBarSherlock, I'm able to customize the background creating the following style on res/values/style.xml and defining it on AndroidManifest.xml as 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 包含下一个代码:

Then, my res/drawable/activated_background.xml contains the next code:

<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中每一行的代码是:

Finally, the code used to define each row in the ListView is:

<?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.

The result is shown on the screenshoot. Is a simple application with only a ListView with ListView.CHOICE_MODE_SINGLE and getListView().setItemChecked(position, true) when list item clicked.

标准选定行的蓝色现在是黄色并且可以完美运行.

The blue color of the standard selected row now is yellow and works perfectly.

当我想使用 ActionBarSherlock 应用相同的自定义时,就会出现问题.现在样式文件是下一个,背景显示为蓝色而不是自定义的黄色.

The problem appears when I want to apply the same customization using the ActionBarSherlock. Now The style file is the next and the background appears blue instead the custom yellow.

<?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 功能,或者我是否忘记实现一些需要能够更改默认颜色的东西.

I don't know if ActionBarSherlock supports the android:activatedBackgroundIndicator feature or if I forgot to implement something needed to be able to change the default color.

有什么想法吗?

推荐答案

终于找到了解决问题的办法.
它是行适配器中使用的 context.使用的构造函数如下代码所示:

Finally I found the solution to the problem.
It was the context used in the row adapter. The constructor used is the shown in the following piece of code:

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()(Application-Context)

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

Just changing the context from:
this.mContext = activity.getApplicationContext()(Application-Context)
to
this.mContext = activity.getBaseContext()(Activity-Context)
solved the problem and now the background is the custom one.

当您需要操作视图时,请使用 Activity-Context,否则 Application-Context 就足够了.

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

这个 answer 帮助我理解了为什么要使用 getBaseContext() 而不是 getApplicationContext() 就我而言.

This answer helped me to understand why to use getBaseContext() instead of getApplicationContext() in my case.

这篇关于ActionBarSherlock 上的activatedBackgroundIndicator 的自定义背景不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆