无法在Android的ToggleButton中使用CustomAttr来应用ColorStateList和TextColor

时间:2019-07-15 13:23:45

标签: android android-layout android-custom-view

我有一个名为SkinableToggleButton的自定义类,该类扩展了Android的ToggleButton。其代码如下:

public class SkinableToggleButton extends ToggleButton {

public SkinableToggleButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public SkinableToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //Applied custom font/typeface here
    init(context, attrs);
}

protected void init(Context context, AttributeSet attrs) {
    if (isInEditMode()) return;

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Skinable, 0, 0);

    try {
        if (a.hasValue(R.styleable.Skinable_skinableTextColor)) {
            int textColorSkin = a.getInt(R.styleable.Skinable_skinableTextColor, 0);
            SkinUtil.applyTextColorTheme(this, textColorSkin);//Applying custom text color through custom attr
        }

        if (a.hasValue(R.styleable.Skinable_skinableSrcTint)) {
int backgroundTintSkin = a.getInt(R.styleable.Skinable_skinableSrcTint, 0);
            int checkedColor = SkinUtil.getColor(backgroundTintSkin);//util method returning correct color
            int disabledColor = ContextCompat.getColor(getContext(), R.color.grey_3);
            int[][] states = new int[][]{
            new int[]{android.R.attr.state_checked}, // checked
            new int[]{-android.R.attr.state_checked}, // unchecked
            new int[]{android.R.attr.state_enabled}, // enabled
            new int[]{-android.R.attr.state_enabled} // disabled
    };
    int[] colors = new int[]{
            checkedColor,
            checkedColor,
            checkedColor,
            disabledColor
    };

    compoundButton.setButtonTintList(new ColorStateList(states, colors));
        }
    } finally {
        a.recycle();
    }
}}

在XML中,我已按如下方式使用它:

<SkinableToggleButton
   android:layout_width="wrap_content"
   style="@style/tiny_toggle_button"
   android:textOff="Connect"
   android:textOn="Connected"/>

样式tiny_toggle_button代码:

<style name="tiny_toggle_button">
    <item name="android:background">@drawable/selector_toggle_btn</item>
    <item name="android:layout_height">@dimen/spacing_5x</item>
    <item name="android:textSize">@dimen/sp_12</item>
    <item name="android:lineHeight">@dimen/sp_16</item>
    <item name="android:letterSpacing">0.0175</item>
    <item name="android:textColor">@color/selector_toggle_bg</item>
    <item name="skinableSrcTint">primary1</item> <!--Custom Attr-->
</style>

selector_toggle_btn可绘制:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:state_checked="false" android:state_enabled="true" >
        <shape
                android:shape="rectangle">
            <solid android:color="@color/transparent"/>
            <corners android:radius="@dimen/spacing_0_5x"/>
            <stroke
                    android:width="1dp"
                    android:color="@color/black"
            />
        </shape>
    </item>
    <item android:state_checked="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/black"/>
            <corners android:radius="@dimen/spacing_0_5x"/>
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape
                android:shape="rectangle">
            <solid android:color="@color/white_1"/>
            <corners android:radius="@dimen/spacing_0_5x"/>
            <stroke
                    android:width="1dp"
                    android:color="@color/grey_4"/>
        </shape>
    </item>
</selector>

selector_toggle_bg的颜色:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:state_enabled="true" android:state_checked="true" android:color="@color/white_1"/>
    <!-- Default State -->
    <item android:state_enabled="true" android:state_checked="false"
          android:color="@color/black"/>
    <item android:color="@color/grey_3" android:state_enabled="false"/>
</selector>

问题: 我的自定义属性(色调颜色和文本颜色)未从选择器可绘制文件和颜色文件中应用。除了我的自定义属性外,我的可绘制和文本颜色选择器工作得很好。我在使用custom attrs并应用ColorStateList时是否做错了?我已经在styles.xml中以自定义样式设置了自定义属性skinableSrcTint。自定义样式的代码已粘贴到上方。

我在StackOverflow上发布问题的经验不是很丰富。请让我知道是否需要其他详细信息,我将对其进行编辑并发布详细信息/或在注释中。

0 个答案:

没有答案