Подтвердить что ты не робот

Создание стиля по умолчанию с настраиваемыми атрибутами

Итак, вот моя проблема: я создал пользовательский компонент, который расширяет Button. Эта кнопка имеет один атрибут testAttr.

<declare-styleable name="TestButton" parent="@android:style/Widget.Button">
    <attr name="testAttr" format="reference|color"/>
</declare-styleable>

Я хочу создать стиль по умолчанию для этого компонента, поэтому я добавил следующее:

<declare-styleable name="TestTheme">
    <attr name="testStyle" format="reference"/>
</declare-styleable>

<style name="Theme.AppTheme" parent="@android:style/Theme" >
    <item name="testStyle">@style/test</item>
</style>

Проблема в том, что я хочу использовать как андроидные атрибуты, так и свои собственные, которые не работают:

<style name="test" parent="@android:style/Widget.Button">
    <item name="android:background">#FF0000</item>
    <item name="testAttr">testText</item>
</style>

Я устанавливаю стили по умолчанию в свой пользовательский компонент следующим образом:

public myButton(final Context context) {
    this(context, null);
}

public myButton(final Context context, final AttributeSet attrs) {
    this(context, attrs, R.styleable.TestTheme_testStyle);
}

public myButton(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);

    final TypedArray array = context.obtainStyledAttributes(
            attrs,
            R.styleable.TestButton,
            defStyle,
            R.style.testDefault);
        final String s = array.getString(R.styleable.TestButton_testAttr);
        setText(s);
        array.recycle();
}

И, конечно, я задаю тему в манифесте:

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppTheme" >

    <activity
        android:name=".TestActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

По какой-то причине он всегда возвращается к стилю по умолчанию и никогда не устанавливает атрибуты android. Так что я здесь что-то пропустил?

4b9b3361

Ответ 1

Хорошо, я должен был выглядеть лучше, прежде чем задавать вопросы...

нашел решение:

заменил это:

public myButton(final Context context, final AttributeSet attrs) {
    this(context, attrs, R.styleable.TestTheme_testStyle);
}

с этим:

public myButton(final Context context, final AttributeSet attrs) {
    this(context, attrs, R.attr.testStyle);
}