2012-07-20 3 views
0

일부 사용자 지정 레이아웃 구성 요소에 문제없이 사용자 지정 특성을 사용하고 있습니다. 지금까지 간단한 속성 (문자열, int 등) 만 사용했습니다.getAttributeResourceValue를 사용할 때 XML 네임 스페이스를 지정해야하는 이유는 무엇입니까?

<declare-styleable name="StaticListView"> 
    <attr name="border_size" format="dimension" /> 
</declare-styleable> 

을 내 레이아웃 : :이 values/attrs.xml에과 같이 정의된다

<de.example.androidapp.StaticListView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     namespace:border_size="1px" 
    /> 

등처럼 사용할 : 이제

int borderSize = (int) a.getDimension(R.styleable.StaticListView_border_size, 0); 

, 나는 같은 레이아웃을 지정하기 위해 노력하고있어 사용자 지정 특성을 사용하고 위에 사용 된 R.styleable 메서드를 사용할 수 없습니다.

<declare-styleable name="StaticListView"> 
    <attr name="emptyLayout" format="reference" /> 
</declare-styleable> 

하고 사용 :

<de.example.androidapp.StaticListView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     namespace:emptyLayout="@layout/empty" 
    /> 

을 그리고 이것은 내가 그것을 사용하고자하는 방법입니다,하지만 난 항상 (기본 가치를 얻을 여기

내가 속성을 정의하고있어 방법 -1) :

int emptyLayoutInt = attrs.getAttributeResourceValue(R.styleable.StaticListView_emptyLayout, -1); 

이를하지만, 작동 :

int emptyLayoutInt = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/de.example.androidapp", "emptyLayout", -1); 

XML 네임 스페이스를 하드 코딩해야하는 것을 좋아하지 않습니다. R.styleable 속성을 사용하면 좋은 방법으로이를 피할 수 있습니다.

내가 잘못했거나 버그/예상되는 동작입니까? 대신 라인을 사용

답변

1

:

int emptyLayoutInt = attrs.getAttributeResourceValue(R.styleable.StaticListView_emptyLayout, -1); 

사용이 -

TypedArray a = context.obtainStyledAttributes(attrs, 
      R.styleable.MyLayout); 
    int layoutId = a.getResourceId(R.styleable.MyLayout_text,-1); 

값이 ATTR 세트에서 사용할 수 없기 때문에 -1이 돌려지고 있습니다.

+0

감사합니다. 내 문제를 해결하지 못했지만 올바르게 사용하지 못했던'TypedArray' 인스턴스를 다시 보게되었습니다. –

+0

환영합니다. getResourceId를 쓰는 것을 잊었습니다. 나는 코드에서 최종 변경을하고있다. –

0

문제점을 파악했습니다. 어떤 이유에서든지 AttributeSet 변수 attrs을 통과하려고했지만 나머지 속성을 사용하는 것처럼 TypedArray 인스턴스를 사용해야했습니다. 다음은 작동하는 코드 줄입니다.

int emptyLayoutInt = a.getResourceId(R.styleable.StaticGridView_emptyLayout, -1); 
관련 문제