Как использовать объявляемый стиль со ссылкой на другой стиль

Я пишу собственный вид, и я также хотел разрешить собственный стиль. У меня проблемы с пониманием того, как использовать формат ссылки на атрибут.

Вот мой attrs.xml

<declare-styleable name="MyButtonControl">
    <attr name="myButtonText" format="string" />
    <attr name="myButtonStyle" format="reference" />
</declare-styleable>

Я попытался использовать myButtonStyle таким образом:

private static final int[] ATTRS = new int[]{
        android.R.attr.text,
        android.R.attr.textAllCaps,
        android.R.attr.textSize,
        android.R.attr.textColor,
        android.R.attr.background
};

private static final int ID_TEXT = 0;
private static final int ID_TEXT_ALL_CAPS = 1;
private static final int ID_TEXT_SIZE = 2;
private static final int ID_TEXT_COLOR = 3;
private static final int ID_BACKGROUND = 4;

public MyButtonControl(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    ...
    ...
    ...
    int buttonStyleId = -1;
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyButtonControl, 0, 0);
    try
    {
        btn.setText(taBLC.getString(R.styleable.MyButtonControl_myButtonText));
        buttonStyleId = ta.getResourceId(R.styleable.MyButtonControl_myButtonStyle, -1);
    }
    finally
    {
        ta.recycle();
    }

    if (buttonStyleId > -1)
    {
        //////////////////////////////////////////////////////////////////
        //
        // The following doesn't work. It always return the default value
        // For example: ta.getColor(ID_TEXT_COLOR, Color.BLACK) always
        // returns Color.Black
        //
        ta = context.obtainStyledAttributes(buttonStyleId, ATTRS);
        try
        {
            btn.setTextSize(ta.getDimensionPixelSize(ID_TEXT_SIZE, 16));
            btn.setAllCaps(ta.getBoolean(ID_TEXT_ALL_CAPS, false));
            btn.setTextColor(ta.getColor(ID_TEXT_COLOR, Color.BLACK));
            btn.setBackgroundResource(ta.getResourceId(ID_BACKGROUND, R.color.color_gray));
        }
        finally
        {
            ta.recycle();
        }
    }
}

По какой-то причине TypedArray стали использовать ta = context.obtainStyledAttributes(buttonStyleId, ATTRS); не найдет никакого значения. Он всегда использует значение по умолчанию.

Любая идея?

Спасибо!

Более полный код:

Вот мой control_my_button.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <Button android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="TEST" />
    ...
    ...
    ...
</RelativeLayout>

Вот как я использую это в моем layout_test_control.xml

<com.digsqua.component.MyButtonControl android:layout_width="wrap_content"
                                       android:layout_height="wrap_content"
                                       app:myButtonText="Testing Text"
                                       app:myButtonStyle="@style/BlueButton" />

Вот мой styles.xml

<style name="BlueButton">
    <item name="android:textAllCaps">false</item>
    <item name="android:textSize">18sp</item>
    <item name="android:background">@drawable/button_blue</item>
    <item name="android:textColor">@drawable/button_blue</item>
</style>

Вот мой button_blue.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@color/button_background_disabled"
          android:color="@color/button_text_color_disabled"/>
    <item android:drawable="@color/button_background_blue"
          android:color="@color/color_white_ghostwhite"/>
</selector>

Вот мой colors.xml

<resources>
    <color name="button_background_disabled">#787878</color>
    <color name="button_background_blue">#234170</color>
    <color name="button_text_color_disabled">#b8b8b8</color>
    <color name="color_white_ghostwhite">#f8f8ff</color>
</resources>

0 ответов

Другие вопросы по тегам