У меня есть настраиваемый элемент управления, который расширяет DialogPreference, где у меня есть настраиваемые атрибуты и Я хочу определить для них значения по умолчанию.
Вот соответствующая часть моего файла attrs.xml:
<!-- definition of my custom attributes -->
<declare-styleable name="MyPreference">
<attr name="myAttr1" format="string" />
<attr name="myAttr2" format="reference" />
</declare-styleable>
<!-- declatation of my style for my AppTheme -->
<declare-styleable name="AppTheme">
<attr name="myPreferenceStyle" format="reference" />
</declare-styleable>
themes.xml:
<style name="AppTheme" parent="@style/Theme.Sherlock.Light.DarkActionBar">
<!-- try of replacing the default text color -->
<item name="android:textAppearance">@style/WhiteText</item>
<item name="myPreferenceStyle">@style/Preference.My</item>
</style>
styles.xml:
<style name="WhiteText" parent="@android:style/TextAppearance">
<!-- set the default color to white... however it doesn't work -->
<item name="android:textColor">#fff</item>
</style>
<style name="Preference">
<item name="android:positiveButtonText">@android:string/ok</item>
<item name="android:negativeButtonText">@android:string/cancel</item>
</style>
<style name="Preference.My">
<item name="android:dialogLayout">@layout/preferences_my_picker</item>
<item name="myAttr1">@string/unknown</item>
<item name="myAttr2">@array/bits</item>
</style>
Итак, я определил, что хочу, чтобы класс MyPreference имел такие значения по умолчанию:
- android:positiveButtonText = "ОК"
- android:negativeButtonText = "Отмена"
- android:dialogLayout = ‹ссылка на макет›
- myAttr1 = "Неизвестно"
- мойАттр2 = [1, 2, 4]
Но когда я пытаюсь получить к ним доступ, я ничего не получаю:
public MyPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyPreference, defStyle, 0);
String txt = a.getText(R.styleable.MyPreference_myAttr1);
// txt == null :(
int bitsResId = a.getResourceId(R.styleable.MyPreference_myAttr2, -1);
// next line will crash bitsResId == -1
int[] bits = res.getIntArray(bitsResId);
a.recycle();
}
public MyPreference(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.myPreferenceStyle);
}
Я был бы очень полезен, если бы кто-нибудь мог объяснить мне, что я делаю неправильно. А также почему я не могу изменить цвет текста по умолчанию на белый.
DialogPreference
? - person user   schedule 27.06.2013