【问题标题】:How to create custom button class and add default features如何创建自定义按钮类并添加默认功能
【发布时间】:2020-07-27 14:13:33
【问题描述】:

我正在尝试创建自定义按钮类,但无法设置默认文本大小和背景。

ButtonGray.kt

class ButtonGray(context: Context?, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatButton(context, attrs) {

    init {
        textSize = 30.0F
        background = R.drawable.bg_btn_gray
    }
}

ButtonGray.kt

text size cannot be assigned by default.

编辑:

我试试这个代码:

 background = context?.getDrawable(R.drawable.bg_btn_gray)

不报错。但默认情况下它不起作用。

enter image description here

【问题讨论】:

  • 为什么不使用样式呢?类应该定义行为。样式应该定义外观。
  • 你能举个例子吗?
  • 官方文档里都有。

标签: java android android-studio kotlin mobile


【解决方案1】:

使用这个类并在初始化部分自定义

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton

class ButtonGray(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :        AppCompatButton(context, attrs, defStyleAttr){

constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
    init()
}
constructor(context: Context) : this(context, null, 0) {
    init()
}

init {
    setBackgroundColor(R.drawable.bg_btn_gray)
    textSize = 30.0F
    text = "Hello"
}}

我有 Java 的实现方式。哪个正在工作 您必须读取背景、大小等属性。在未设置的情况下设置一些默认值并将这些设置为按钮,如下面的代码

读取属性

TypedArray attributes = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.IGButton,0,0);
    try {
        setType(attributes.getInt(R.styleable.IGButton_buttonType,0));
        setiConPos(attributes.getInt(R.styleable.IGButton_iconPos,0));
        borderWidth = attributes.getInt(R.styleable.IGButton_borderWidth, Constants.BUTTON_BORDER_WIDTH);
        radius = attributes.getInt(R.styleable.IGButton_borderRadius, Constants.BUTTON_BORDER_RADIUS);
        fontSize = attributes.getInt(R.styleable.IGButton_fontSizeSP, Constants.BUTTON_TEXT_FONT_SIZE);
        int drawableId = attributes.getResourceId(R.styleable.IGButton_smallIcon, 0);
        if(drawableId > 0) {
            icon = getResources().getDrawable(drawableId, context.getTheme());
        }
    }
    finally {
        attributes.recycle();
    }

设置成这样

this.setTextColor(textColor);
this.setAllCaps(false);
    this.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    ((Button)this).setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);

设置自定义背景

GradientDrawable drawable = new GradientDrawable();
    drawable.setColor(backgroundColor);
    drawable.setCornerRadius(radius);
    drawable.setStroke(borderWidth, borderColor);
    this.setBackground(drawable);
    StateListAnimator animator =  AnimatorInflater.loadStateListAnimator(context,
            R.animator.button_animation_set);
    ((Button)this).setStateListAnimator(animator);

【讨论】:

  • 我更新了答案。 java中的一些代码你可能必须用kotlin编写。
【解决方案2】:
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
   
class ButtonGray : AppCompatButton {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {

        width= resources.getDimension(R.dimen.your_dimen).toInt()
        height= resources.getDimension(R.dimen.your_dimen).toInt()

        setBackgroundColor(ContextCompat.getColor(context,R.color.yourColor))

    }
}

【讨论】:

  • 欢迎使用 Stackoverflow,请考虑写一两句话来描述您的解决方案。
【解决方案3】:

这是我在 Kotlin 中创建自定义按钮的方式。

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import com.urfeed.R


class PrimaryButton : AppCompatButton {

    constructor(context: Context) : super(context){
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        init()
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        init()
    }


    private fun init() {
        this.background = context.getDrawable(R.drawable.gradient)
        this.textSize = 10f
        this.setTextColor(context.getColor(R.color.white))
    }

    fun turnIntoPrimary(text: String){
        this.text = text
        this.background = context.getDrawable(R.drawable.primary_bkg_drawable)
        postInvalidate()
    }

     fun turnIntoSecondary(text: String){
        this.text = text
        this.background = context.getDrawable(R.drawable.secondary_bkg_drawable)
        postInvalidate()
    }

}

用法:

button.turnIntoPrimary("Primary")

button.turnIntoSecondary("次要")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2017-06-14
    • 1970-01-01
    • 2020-05-24
    相关资源
    最近更新 更多