从 android

问题描述

我正在学习在 android 中创建自定义视图。我创建了名为 JFXButton 的组件。但是我在使用 JFXButton 的 setTextColor() 时遇到了问题。当我尝试从 xml 文件替换颜色时,它不会改变颜色。 JFXButton 扩展了 AppCompactButton。建议我正确的方法

这是完整的代码

JFXButton.java


public class JFXButton extends AppCompatButton {

    @DrawableRes
    private static final int DEFAULT_BUTTON = R.drawable.common_primary_btn;

    private ButtonDesignType mButtonDesignType;
    private int mChangeColor;

    public JFXButton(Context context) {
        super(context);
        init(context,null,0);
    }

    public JFXButton(Context context,AttributeSet attrs) {
        super(context,attrs);
        init(context,attrs,AttributeSet attrs,int defStyle) {
        super(context,defStyle);
        init(context,defStyle);
    }

    private void init(Context context,int defStyle) {
        // Load attributes
        final TypedArray a = context.obtainStyledAttributes(
                attrs,R.styleable.JFXButton,defStyle,0);

        if (attrs == null) {
            return;
        }
        
        mButtonDesignType = ButtonDesignType.get(a.getInt(R.styleable.JFXButton_buttonDesignType,0));

        if (!a.hasValue(R.styleable.JFXButton_buttonDesignType)) {
            setBackground(getResources().getDrawable(DEFAULT_BUTTON));
        }
        setButtonDesign();
        setTextColor(Color.GREEN);
        a.recycle();

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void setButtonDesign() {
        if (mButtonDesignType == ButtonDesignType.Cancel) {
            setBackground(getResources().getDrawable(R.drawable.common_cancel_btn));
        } else if (mButtonDesignType == ButtonDesignType.SaveAndClose) {
            setBackground(getResources().getDrawable(R.drawable.common_save_and_close_btn));
        } else if (mButtonDesignType == ButtonDesignType.Save) {
            setBackground(getResources().getDrawable(R.drawable.common_save_btn));
        } else if (mButtonDesignType == ButtonDesignType.Close) {
            setBackground(getResources().getDrawable(R.drawable.common_close_btn));
        } else if (mButtonDesignType == ButtonDesignType.Primary) {
            setBackground(getResources().getDrawable(R.drawable.common_primary_btn));
        } else if (mButtonDesignType == ButtonDesignType.Plain) {
            setBackground(getResources().getDrawable(R.drawable.common_plain_btn));
        } else if (mButtonDesignType == ButtonDesignType.Done) {
            setBackground(getResources().getDrawable(R.drawable.done_cancelv3_btn));
        } else if (mButtonDesignType == ButtonDesignType.CancelV3) {
            setBackground(getResources().getDrawable(R.drawable.done_cancelv3_btn));
        }
    }


    public ButtonDesignType getmButtonDesignType() {
        return mButtonDesignType;
    }

    public void setmButtonDesignType(ButtonDesignType mButtonDesignType) {
        this.mButtonDesignType = mButtonDesignType;
        invalidate();
        requestLayout();
    }
}


attr_jfx_button.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="JFXButton">
        <attr name="buttonDesignType" format="enum">
            <enum name="Cancel" value="0"/>
            <enum name="SaveAndClose" value="1"/>
            <enum name="Save" value="2"/>
            <enum name="Close" value="3"/>
            <enum name="Primary" value="4"/>
            <enum name="Plain" value="5"/>
            <enum name="Done" value="6"/>
            <enum name="CancelV3" value="7"/>
        </attr>
    </declare-styleable>
</resources>

layout_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.uniberse.unibersereataurent.ui.JFXButton
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        android:text="JFXButton"
        android:textColor="@color/white"
        android:textSize="@dimen/_18sdp"
        app:buttonDesignType="Done" />


</LinearLayout>

帮我解决这个问题。 谢谢

解决方法

在按钮的xml中添加app:backgroundTint="@null"

,

我找到了解决方案。 我们需要在我们的属性文件中定义没有格式的 Button 属性。我们可以从 xml 更改颜色。所以最终代码如下:

JFXButton.java


public class JFXButton extends AppCompatButton {

    @DrawableRes
    private static final int DEFAULT_BUTTON = R.drawable.common_primary_btn;

    private ButtonDesignType mButtonDesignType;
    private int mChangeColor;

    public JFXButton(Context context) {
        super(context);
        init(context,null,0);
    }

    public JFXButton(Context context,AttributeSet attrs) {
        super(context,attrs);
        init(context,attrs,AttributeSet attrs,int defStyle) {
        super(context,defStyle);
        init(context,defStyle);
    }

    private void init(Context context,int defStyle) {
        // Load attributes
        final TypedArray a = context.obtainStyledAttributes(
                attrs,R.styleable.JFXButton,defStyle,0);

        if (attrs == null) {
            return;
        }
        
        mButtonDesignType = ButtonDesignType.get(a.getInt(R.styleable.JFXButton_buttonDesignType,0));
        int textColor = a.getColor(R.styleable.JFXButton_android_textColor,Color.BLACK);

        if (!a.hasValue(R.styleable.JFXButton_buttonDesignType)) {
            setBackground(getResources().getDrawable(DEFAULT_BUTTON));
        }
        setButtonDesign();
        setTextColor(textColor);
        a.recycle();

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void setButtonDesign() {
        if (mButtonDesignType == ButtonDesignType.Cancel) {
            setBackground(getResources().getDrawable(R.drawable.common_cancel_btn));
        } else if (mButtonDesignType == ButtonDesignType.SaveAndClose) {
            setBackground(getResources().getDrawable(R.drawable.common_save_and_close_btn));
        } else if (mButtonDesignType == ButtonDesignType.Save) {
            setBackground(getResources().getDrawable(R.drawable.common_save_btn));
        } else if (mButtonDesignType == ButtonDesignType.Close) {
            setBackground(getResources().getDrawable(R.drawable.common_close_btn));
        } else if (mButtonDesignType == ButtonDesignType.Primary) {
            setBackground(getResources().getDrawable(R.drawable.common_primary_btn));
        } else if (mButtonDesignType == ButtonDesignType.Plain) {
            setBackground(getResources().getDrawable(R.drawable.common_plain_btn));
        } else if (mButtonDesignType == ButtonDesignType.Done) {
            setBackground(getResources().getDrawable(R.drawable.done_cancelv3_btn));
        } else if (mButtonDesignType == ButtonDesignType.CancelV3) {
            setBackground(getResources().getDrawable(R.drawable.done_cancelv3_btn));
        }
    }


    public ButtonDesignType getmButtonDesignType() {
        return mButtonDesignType;
    }

    public void setmButtonDesignType(ButtonDesignType mButtonDesignType) {
        this.mButtonDesignType = mButtonDesignType;
        invalidate();
        requestLayout();
    }
}


attr_jfx_button.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="JFXButton">
        <attr name="buttonDesignType" format="enum">
            <enum name="Cancel" value="0"/>
            <enum name="SaveAndClose" value="1"/>
            <enum name="Save" value="2"/>
            <enum name="Close" value="3"/>
            <enum name="Primary" value="4"/>
            <enum name="Plain" value="5"/>
            <enum name="Done" value="6"/>
            <enum name="CancelV3" value="7"/>
        </attr>
        <attr name="android:textColor" />
        <attr name="android:textSize" />
        <attr name="android:text" />
        <attr name="android:background" />
    </declare-styleable>
</resources>

layout_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.uniberse.unibersereataurent.ui.JFXButton
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        android:text="JFXButton"
        android:textColor="@color/white"
        android:textSize="@dimen/_18sdp"
        app:buttonDesignType="Done" />


</LinearLayout>