Пользовательский фон для activeBackgroundIndicator на ActionBarSherlock не работает
Я использую ActionBarSherlock, и я пытаюсь настроить атрибут activeBackgroundIndicator для фона строки.
Если я использую последний sdk для Android, без 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 (позиция, true) при нажатии на элемент списка.
Синий цвет стандартного выделенного ряда теперь желтый и отлично работает.
![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, или я забыл реализовать что-то, что необходимо для изменения цвета по умолчанию.
Любые идеи?
Ответы
Ответ 1
Наконец, я нашел решение проблемы.
Это был контекст, используемый в адаптере строки. Используемый конструктор показан в следующем фрагменте кода:
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)
решили проблему, и теперь фон является обычным.
Всякий раз, когда вам нужно манипулировать представлениями, переходите к Activity-Context, иначе Application-Context будет достаточно.
Этот ответ помог мне понять, почему использовать getBaseContext() вместо getApplicationContext() в моем случае.
![custom row background]()