본문 바로가기

안드로이드

AttributeSet

반응형

Android XML에서 View를 생성할 때 값을 설정할 수 있는 AttributeSet 인터페이스를 제공합니다. AttributeSet을 통해 View를 생성하면 기본 값을 쉽게 설정할 수 있습니다.

attr.xml 에 선언하여 만들 수 있습니다. 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textColor" format="reference|color" />

    <declare-styleable name="ProgressView">
        <attr name="maxValue" format="reference|integer" />
        <attr name="value" format="reference|integer" />
        <attr name="textSize" format="reference|dimension" />
        <attr name="textColor" />
        <attr name="progressBarBackgroundColor" format="reference|color" />
        <attr name="progressBarColor" format="reference|color" />
        <attr name="progressBarWidth" format="reference|dimension" />
    </declare-styleable>

</resources>

이렇게 받아서 사용할 수 있습니다.

        context.theme.obtainStyledAttributes(attrs, R.styleable.ProgressView, defStyleAttr, defStyleRes).apply {
            maxValue = getInt(R.styleable.ProgressView_maxValue, maxValue)
            value = getInt(R.styleable.ProgressView_value, value)
            textSize = getDimension(R.styleable.ProgressView_textSize, textSize)
            textColor = getColor(R.styleable.ProgressView_textColor, textColor)
            progressBarBackgroundColor = getColor(R.styleable.ProgressView_progressBarBackgroundColor, progressBarBackgroundColor)
            progressBarColor = getColor(R.styleable.ProgressView_progressBarColor, progressBarColor)
            progressBarWidth = getDimension(R.styleable.ProgressView_progressBarWidth, progressBarWidth)
        }

 

참고 사이트 : https://rkdxowhd98.tistory.com/162

반응형