Android : 여백을 라디오 버튼 드로어 블의 왼쪽으로 설정
Jan 10 2021
내 라디오 버튼 드로어 블과 그 왼쪽 사이에 여백 / 패딩을 설정하고 싶습니다. 예 : 라디오 버튼 드로어 블과 화면 왼쪽 사이에 8dp 여백 설정. 라디오 버튼 자체 사이에 여백을 설정하는 방법을 알고 있지만 라디오 버튼 드로어 블을 사용하는 방법은 없습니다. 또한 paddngStart = "YOUR_PADDING"을 사용하여 라디오 버튼 드로어 블의 오른쪽 여백을 설정하는 방법도 알고 있습니다.
이것이 가능한가?
여기 내가 의미하는 바에 대한 그림이 있습니다.
현재
![](https://post.nghiatu.com/assets/images/s/7GxdQ.png)
내가 원하는 것
![](https://post.nghiatu.com/assets/images/s/ofbI2.png)
편집하다
위의 서면 답변이 작동합니다. 프로그래밍 방식이 아닌 레이아웃 내부의 값을 설정하려는 사람들을 위해 바인딩 어댑터를 작성했습니다.
@BindingAdapter("setDrawableLeftPadding")
fun setDrawableLeftPadding(view: CustomRadioButton, padding: Float) {
view.setStartPaddingDp(padding)
}
그런 다음 CustomRadioButton 레이아웃 내에서 사용할 수 있습니다. app:setDrawableLeftPadding="@{8f}"
답변
1 mhdwajeeh.95 Jan 10 2021 at 23:20
두 번째 사진이 보여주는 것과 똑같은 결과를 얻고 싶습니다. 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)
}
}
필드 값을 설정하여 패딩 값을 설정할 수 있습니다 startPadding
. 예를 들면 다음과 같습니다.
yourCustomRadioButton.startPadding = 100 // this value is in pixels
// or
yourCustomRadioButton.setStartPaddingDp(100) // this value is in DP