自定义背景activatedBackgroundIndicator上ActionBarSherlock不起作用 [英] Custom background for activatedBackgroundIndicator on ActionBarSherlock doesn't work
问题描述
我用 ActionBarSherlock ,然后我想自定义的 activatedBackgroundIndicator 的属性为行背景。
I'm using ActionBarSherlock and I'm trying to customize the activatedBackgroundIndicator attribute for the row background.
如果我用的是最新的Android SDK中,没有在 ActionBarSherlock ,我能够创建下面的样式自定义背景的 RES /价值/ style.xml 的和在定义它的的AndroidManifest.xml 的作为的安卓主题=@风格/ 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 /绘制/ activated_background.xml 的包含下code:
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定义每一行的code是:
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>
结果示于screenshoot。是只有具有ListView控件的简单应用程序的 ListView.CHOICE_MODE_SINGLE 和 getListView()。setItemChecked(位置,真)当列表项点击。
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 支持的机器人: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.
任何想法?
推荐答案
我终于找到了解决问题的办法。
这是背景在行适配器一起使用。所使用的构造是显示在下面的一段code:
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()
(应用程序上下文)
到
this.mContext = activity.getBaseContext()
(以活动为背景)
解决了这个问题,现在的背景是自定义的
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.
当你需要处理意见,然后去活动为背景,否则应用上下文就足够了。
Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.
这回答帮助我了解为什么要使用替代的 getBaseContext()的 getApplicationContext()的在我的情况。
This answer helped me to understand why to use getBaseContext() instead of getApplicationContext() in my case.
这篇关于自定义背景activatedBackgroundIndicator上ActionBarSherlock不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!