具有不同大小的customView

时间:2018-09-11 08:43:23

标签: android kotlin android-custom-view android-constraintlayout

我有一个customView,希望用作自定义按钮。xml如下:

//customButton.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/custom_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <TextView
        android:id="@+id/CB_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:textAlignment="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

这在我的costumButton类中得到了夸大,我可以在其中创建圆角按钮或具有所需颜色的圆形按钮:

    private var rectF: RectF? = null
private fun initView(context: Context, attrs: AttributeSet?) {
    this.setWillNotDraw(false)
    this.attrs = attrs


    val view = View.inflate(context, R.layout.custom_button, null)
    val a = context.theme
            .obtainStyledAttributes(attrs, R.styleable.CustomizedButton, 0, 0)

    addView(view)
    this.rectF = RectF()
    try {
        this.placeInGroup = a.getInt(R.styleable.CustomizedButton_CB_place_in_group, 0)
        this.shape = a.getString(R.styleable.CustomizedButton_CB_shape)
        this.radius = a.getFloat(R.styleable.CustomizedButton_CB_radius, 0f)
        this.textColor = a.getColor(R.styleable.CustomizedButton_CB_text_color, Color.BLACK)
        CB_text.setTextColor(textColor)
        CB_text.textSize = a.getDimension(R.styleable.CustomizedButton_CB_text_size, 10f)
        this.strokeColor = a.getColor(R.styleable.CustomizedButton_CB_strokeColor,-1)
        this.fillColor = a.getColor(R.styleable.CustomizedButton_CB_fillColor, -1)
        this.fill = a.getBoolean(R.styleable.CustomizedButton_CB_fill, false)
        this.strokeWidth = a.getFloat(R.styleable.CustomizedButton_CB_strokeWidth, 0f)
        CB_text.text = a.getText(R.styleable.CustomizedButton_CB_text)?:  ""

    } finally {
        a.recycle()
    }


}

val brush1 = Paint()
val offset = 0f

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    val widthVar = width
    val hieghtVar = height
    brush1.color = if (fillColor!=-1) fillColor else if (strokeColor!=-1) strokeColor else resources.getColor(neutralBackground)
    brush1.strokeWidth = strokeWidth
    brush1.style = if (fill) Paint.Style.FILL else Paint.Style.STROKE

    if (shape == "Circle"){

        canvas.drawCircle(((widthVar / 2)).toFloat(), (hieghtVar / 2).toFloat(), (hieghtVar-brush1.strokeWidth)/2f, brush1)
    }else{
        rectF?.let{
            it.set(offset, offset, width-offset,height-offset)
            canvas.drawRoundRect(rectF,radius, radius, brush1)
        }
    }


}

此处的属性在单独的xml中给出,其中我使用了customButton标记。我希望在customButton标记中也定义大小。例如,在键盘情况下,按键应为圆形,因此宽度和高度应为1:1的比例,而在其他情况下则不建议这样做。下面的xml用于以编程方式创建键盘的9个键,应将其添加到片段中的customButtonGroup中。

//keyboard.xml
<assa.ux.CustomButton
xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="1:1"
android:clickable="true"
android:focusable="true"
android:visibility="visible"
app:CB_text_size="24dp"
app:CB_shape = "Circle"
app:CB_strokeWidth="10"
app:CB_strokeColor="#a2d032">
</assa.ux.CustomButton>

但是这不起作用,并且键仅包装内容。我试图更改代码的大小,但还是没有运气。

0 个答案:

没有答案