简体   繁体   English

Android:将边距设置为可绘制单选按钮的左侧

[英]Android: Set margin to left side of radiobutton drawable

I want to set a margin / padding between my radiobutton drawable and the left side of it, eg: Setting a margin of 8dp between my radiobutton drawable and the left side of the screen.我想在我的radiobutton drawable 和它的左侧之间设置一个边距/填充,例如:在我的radiobutton drawable 和屏幕左侧之间设置一个8dp 的边距。 I know how to set a margin between the radiobutton itself, but not how to do it with the radiobutton drawable.我知道如何在单选按钮本身之间设置边距,但不知道如何使用单选按钮可绘制。 I also know how to set a margin right side of the radiobutton drawable with paddngStart="YOUR_PADDING".我也知道如何使用 paddngStart="YOUR_PADDING" 设置可绘制单选按钮右侧的边距。

Is this possible?这可能吗?

Here a picture of what I mean:这是我的意思的图片:

Currently目前

在此处输入图像描述

What I want我想要的是

在此处输入图像描述

EDIT编辑

The aboven written answer does work.上面的书面答案确实有效。 For those wo want to set the value inside the layout and not programmatically, I have written a binding adapter:对于那些想要在布局中设置值而不是以编程方式设置值的人,我编写了一个绑定适配器:

@BindingAdapter("setDrawableLeftPadding")
fun setDrawableLeftPadding(view: CustomRadioButton, padding: Float) {
    view.setStartPaddingDp(padding)
}

You can then use it inside your CustomRadioButton layout with app:setDrawableLeftPadding="@{8f}"然后,您可以通过app:setDrawableLeftPadding="@{8f}"在 CustomRadioButton 布局中使用它

I you want to achieve the exact same on what your second picture shows, you can write a custom RadioButton that handle this padding, the custom view code can be like this (in Kotlin) :我想在第二张图片上实现完全相同的效果,您可以编写一个自定义RadioButton来处理此填充,自定义视图代码可以是这样的(在 Kotlin 中)

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatRadioButton

class CustomRadioButton : AppCompatRadioButton {

    // the value of your padding (in pixels) from the start of the radio button
    var startPadding: Int = 0
        get
        set(value) {
            field = value
            requestLayout()
        }

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


    fun setStartPaddingDp(paddingDp: Float) {
        startPaddingPx = (paddingDp * context.resources.displayMetrics.density).toInt()
    }


    override fun onDraw(canvas: Canvas?) {
        // todo: handle Right-To-Left layouts
        canvas?.translate(startPadding.toFloat(), 0f)

        super.onDraw(canvas)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        setMeasuredDimension(measuredWidth + startPadding, measuredHeight)
    }
}

You can set the padding value by setting the value of the field startPadding , for example:您可以通过设置字段startPadding的值来设置填充值,例如:

yourCustomRadioButton.startPadding = 100 // this value is in pixels

// or

yourCustomRadioButton.setStartPaddingDp(100) // this value is in DP

val compoundButtonDrawable = CompoundButtonCompat.getButtonDrawable(radioButton) val insetDrawable = InsetDrawable(compoundButtonDrawable, 32, 0, 0, 0) radioButton.buttonDrawable = insetDrawable val complexButtonDrawable = CompoundButtonCompat.getButtonDrawable(radioButton) val insetDrawable = InsetDrawable(compoundButtonDrawable, 32, 0, 0, 0) radioButton.buttonDrawable = insetDrawable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM