Nice programing

AttributeSet은 무엇이며 어떻게 사용할 수 있습니까?

nicepro 2020. 10. 30. 21:01
반응형

AttributeSet은 무엇이며 어떻게 사용할 수 있습니까?


Android의 AttributeSet은 무엇입니까?

내 사용자 정의보기에 어떻게 사용할 수 있습니까?


다른 사람들에게는 자세한 설명이지만 늦은 답변.

AttributeSet (Android 문서)

XML 문서의 태그와 연관된 속성 모음입니다.

기본적으로 사용자 정의보기를 만들려고하고 치수, 색상 등과 같은 값을 전달하려는 경우 AttributeSet.

여기에 예가 있습니다.

View아래와 같이 만들고 싶다고 상상해보십시오.

여기에 이미지 설명 입력

노란색 배경이있는 직사각형과 그 안에 원이 있고 반경이 5dp이고 배경이 녹색이라고 가정 해 보겠습니다. 뷰가 다음과 같이 XML을 통해 배경색 및 반경 값을 가져 오도록하려면 다음과 같이하십시오.

<com.anjithsasindran.RectangleView
    app:radiusDimen="5dp"
    app:rectangleBackground="@color/yellow"
    app:circleBackground="@color/green" />

그것이 AttributeSet사용되는 입니다. attrs.xml다음 속성을 사용하여 값 폴더 에이 파일 저장할 수 있습니다 .

<declare-styleable name="RectangleViewAttrs">
    <attr name="rectangle_background" format="color" />
    <attr name="circle_background" format="color" />
    <attr name="radius_dimen" format="dimension" />
</declare-styleable>

이것이 View이기 때문에 Java 클래스는 View

public class RectangleView extends View {

    public RectangleView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.RectangleViewAttrs);
        mRadiusHeight = attributes.getDimensionPixelSize(R.styleable.RectangleViewAttrs_radius_dimen, getDimensionInPixel(50));
        mCircleBackgroundColor = attributes.getDimensionPixelSize(R.styleable.RectangleViewAttrs_circle_background, getDimensionInPixel(20));
        mRectangleBackgroundColor = attributes.getColor(R.styleable.RectangleViewAttrs_rectangle_background, Color.BLACK);
        attributes.recycle()
    }
}

이제 RectangleViewXML 레이아웃에서 이러한 속성을 사용할 수 있으며 RectangleView생성자 에서 이러한 값을 얻을 수 있습니다 .

app:radius_dimen
app:circle_background
app:rectangle_background

AttributeSet은 xml 리소스 파일에 지정된 속성 집합입니다. 사용자 정의보기에서 특별한 작업을 수행 할 필요가 없습니다. View(Context context, AttributeSet attrs)레이아웃 파일에서 뷰를 초기화하기 위해 호출됩니다. 이 생성자를 사용자 정의보기에 추가하기 만하면됩니다. SDK Custom View 예제를 확인하여 사용되었는지 확인하십시오.


AttributeSet을 사용하여 xml에서 정의한 뷰에 대한 추가 사용자 지정 값을 가져올 수 있습니다. 예를 들면. "AttributeSet에서 직접 값을 읽을 수 있습니다"라고 설명하는 사용자 지정 속성 정의에 대한 자습서가 있지만 실제로이를 수행하는 방법에 대해서는 설명하지 않습니다. 그러나 스타일이 지정된 속성을 사용하지 않으면 다음과 같이 경고 합니다.

  • 속성 값 내의 리소스 참조가 확인되지 않습니다.
  • 스타일이 적용되지 않음

스타일이 지정된 전체 속성을 무시하고 속성을 직접 가져 오려면 다음을 수행하십시오.

example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://www.chooseanything.org">

  <com.example.CustomTextView
    android:text="Blah blah blah"
    custom:myvalue="I like cheese"/>

</LinearLayout>

두 줄의 xmlns (xmlns = XML 네임 스페이스)가 있고 두 번째 줄은 xmlns : custom으로 정의됩니다. 그런 다음 해당 custom : myvalue 아래에 정의됩니다.

CustomTextView.java

public CustomTextView( Context context, AttributeSet attrs )
{
  super( context, attrs );
  String sMyValue = attrs.getAttributeValue( "http://www.chooseanything.org", "myvalue" );
  // Do something useful with sMyValue
}

XML 레이아웃에서 뷰가 생성되면 XML 태그의 모든 속성은 리소스 번들에서 읽어와 뷰의 생성자에 AttributeSet

AttributeSet직접 값을 읽을 수 있지만 이렇게하면 몇 가지 단점이 있습니다.

  • 속성 값 내의 리소스 참조가 확인되지 않습니다.
  • 스타일이 적용되지 않음

대신 통과 AttributeSetobtainStyledAttribute(). 이 메서드는 TypedArray종속되고 스타일이 지정된 값의 배열을 다시 전달 합니다.

참고 URL : https://stackoverflow.com/questions/5316686/what-is-attributeset-and-how-can-i-use-it

반응형