TypedArray не работает

Я изучаю настраиваемые компоненты, и у меня возникли проблемы с настраиваемыми атрибутами xml.
Мой настраиваемый компонент расширяет LinearLayout и в конструкторе ( public Custom (Context context, AttributeSet attrs) ) i Я раздуваю XML-макет (2 кнопки и 1 EditText).
Я также объявил в values ​​/ attrs эти настраиваемые атрибуты:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Custom">
        <attr name="initValue" format="integer" />
        <attr name="stepSize" format="integer" />
        <attr name="maxValue" format="integer"/>
    </declare-styleable>
</resources>


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

   if (attrs != null) {
                TypedArray ta = context.obtainStyledAttributes(attrs,
                        R.styleable.Custom, 0, 0);
                setInitValue(ta.getInt(R.styleable.Custom_initValue, 0));
                setStepSize(ta.getInt(R.styleable.Custom_stepSize, 1));
                setMaxValue(ta.getInt(R.styleable.Custom_maxValue, 5));         
                ta.recycle();
            }


Теперь я пытаюсь протестировать этот настраиваемый компонент, добавляя его в макет xml следующим образом:

<?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="fill_parent">
    <here.is.my.package.Custom android:id="@+id/add"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        initValue="2" maxValue="7" stepSize="1" />
</LinearLayout>


T его не работает, и я получаю только значения по умолчанию (0, 1, 5). Я что-то упускаю или это нормальное поведение?

6
задан Diego Palomar 24 March 2016 в 15:01
поделиться