programing

styles.xml의 사용자 지정 특성

lastcode 2023. 9. 4. 20:08
반응형

styles.xml의 사용자 지정 특성

사용자 지정 위젯을 생성하여 layout.xml에 선언합니다.또한 tr.xml에 사용자 지정 특성을 추가했습니다. styles"styles.xml"과 "styles.xml"이 됩니다.No resource found that matches the given name: attr 'custom:attribute'.

나는 그것을 넣었습니다.xmlns:custom="http://schemas.android.com/apk/res/com.my.package"styles.xml을 한 styles<?xml>,<resources>,그리고.<style>사용자 지정 XML 네임스페이스를 찾을 수 없다는 동일한 오류가 여전히 발생합니다.

그러나 네임스페이스를 사용하여 layout.xml의 뷰에 속성을 수동으로 할당할 수 있으므로 네임스페이스에는 아무런 문제가 없습니다.나의 문제는 styles.xml이 나의 attr.xml을 인식하게 하는 것입니다.

알아냈어요!스타일에 네임스페이스를 지정하지 않는 이 정답입니다.

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>

        <item name="custom_attr">value</item> <!-- tee hee -->
    </style>
</resources>

위의 답변은 저를 위해 효과가 있습니다. 저는 약간의 변화를 시도했고, 저는 자원 요소의 수업에 스타일리쉬하다고 선언합니다.

<declare-styleable name="VerticalView">
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
    <attr name="textBold" format="boolean" />
</declare-styleable>

declare-styleable에서 이름 속성은 클래스 이름을 참조했기 때문에 "com.my.package.name "이라는 뷰 클래스를 받았습니다.VerticalView"(수직 뷰)라는 항목은 VerticalView 또는 VerticalView 하위 클래스에서 이 선언을 사용해야 함을 나타냅니다.그래서 우리는 다음과 같은 스타일을 선언할 수 있습니다.

<resources>
    <style name="verticalViewStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">36dip</item>

        <item name="textSize">28sp</item>  <!-- not namespace prefix -->
        <item name="textColor">#ff666666</item>
        <item name="textBold">true</item>
    </style>
</resources>

그렇기 때문에 리소스 요소에서 네임스페이스를 선언하지 않았습니다. 여전히 작동합니다.

values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="defaultButtonColor">@color/red</item>
    <item name="defaultButtonHeight">@dimen/dp_100</item>
</style>

values/attrs.xml

<resources>
    <attr name="defaultButtonColor" format="reference" />
    <attr name="defaultButtonHeight" format="reference"/>
</resources>

values/values.xml

<resources>
    <color name="red">#f00</color>
</resources>

values/dimens.xml

<resources>
    <dimen name="dp_100">100dp</dimen>
</resources>

사용.

<Button
    android:layout_width="wrap_content"
    android:layout_height="?attr/defaultButtonHeight"
    android:text="Button"
    android:textColor="?attr/defaultButtonColor"
    />

enter image description here

데모

스타일러와 빈스의 수정은 저에게 효과가 있었습니다.저는 @vince의 설명이 완전히 정확하지 않을 수도 있다는 것을 지적하고 싶었습니다.

이속 다이같경의 이름 이 름우은라는 .declare-styleable 수 . 사용자 지정 보기 클래스의 했습니다. 이름을 변경했습니다.declare-styleable 정의 이 지정되었습니다.)TestViewFont:

<declare-styleable name="TextViewFont2">
    <attr name="font" format="integer"/>
</declare-styleable>

그리고 나서 저는 그것을 바꿨습니다.obtainStyledAttributes이를 반영하기 위해 사용자 지정 보기를 호출합니다.

TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TextViewFont2, 0, 0);

코드는 여전히 실행 중입니다.그래서 저는 그것이 어떤 종류의 검찰의 내사라고 생각하지 않습니다.declare-styleable클래스의 이름을 따서 명명되었습니다.

따라서 네임스페이스를 참조하지 않고 사용자 지정 특성을 사용하여 스타일을 선언할 수 있다고 생각합니다.

그럼에도 불구하고, 모든 도움을 주셔서 감사합니다, 그것은 제 문제를 해결했습니다.

  • 일부 특성 정의

<declare-styleable name="refreshPullRefreshLayout">
        <attr name="refreshColors" format="reference"/>
        <attr name="refreshColor" format="reference"/>
</declare-styleable>
  • 레이아웃 파일에서 다음과 같이 사용

<com.aolphn.PullRefreshLayout
     app:refreshColor="@color/main_green"
     app:refreshColors="@array/refresh_color"/>
  • 마지막으로 스타일 파일에서 사용합니다.

    스타일 파일과 레이아웃 파일의 차이점은 프리픽스를 추가하지 않는다는 것입니다.app:
<style name="refreshStyle">
    <item name="refreshColor">@color/main_green</item>
    <item name="refreshColors">@array/refresh_color</item>
</style>

해봐요, 좋은 하루 보내요, 이건 나한테 효과가 있어요.

다른 사용자에게 도움이 될 경우를 대비하여, 제 실수는 사용자 정의 뷰 클래스가 AttributeSet.getAttributeValue를 호출한 것입니다.

String fontName = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "customFont");

...이로 인해 사용자 지정 특성이 사용자 지정 보기에 대해 읽혀지지 않았습니다.

해결책은 다음과 같습니다.obtainStyledAttributes내 사용자 정의 보기에서:

 TypedArray styleAttrs = context.obtainStyledAttributes(attrs, R.styleable.MyTextViewStyleable);
 String fontName = styleAttrs.getString(R.styleable.MyTextViewStyleable_customFont);

이것이 올바르게 작동한다는 힌트는 Ctrl/App + 를 클릭할 수 있다는 것입니다.R.styleable.MyTextViewStyleable_customFontattrs.xml 정의로 바로 이동합니다.

사용자 지정 특성이 스타일 대신 레이아웃 XML을 통해 직접 전달될 때 제대로 작동했기 때문에 코드와 다른 예제 간에 이러한 중요한 차이점을 발견하는 데 시간이 걸렸습니다.

언급URL : https://stackoverflow.com/questions/6860886/custom-attributes-in-styles-xml

반응형