2011-09-19 2 views
5

사용자 지정 구성 요소에 대해 배우고 있는데 사용자 지정 XML 특성에 문제가 있습니다.
내 사용자 지정 구성 요소는 LinearLayout을 확장하고 생성자 (public Custom(Context context, AttributeSet attrs))에서 xml 레이아웃 (2 개의 단추와 1 개의 EditText)을 부 풀리고 있습니다.
나는 또한 values/attrs 선언이 사용자 정의 속성 :
TypedArray가 작동하지 않습니다.

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Custom"> 
     <attr name="initValue" format="integer" /> 
     <attr name="stepSize" format="integer" /> 
     <attr name="maxValue" format="integer"/> 
    </declare-styleable> 
</resources> 


을 내가 정의를 읽기 위해 노력하고있어 레이아웃을 부풀려 후 생성자에서이 같은 속성 : 이제

if (attrs != null) { 
       TypedArray ta = context.obtainStyledAttributes(attrs, 
         R.styleable.Custom, 0, 0); 
       setInitValue(ta.getInt(R.styleable.Custom_initValue, 0)); 
       setStepSize(ta.getInt(R.styleable.Custom_stepSize, 1)); 
       setMaxValue(ta.getInt(R.styleable.Custom_maxValue, 5));   
       ta.recycle(); 
      } 


전 이 맞춤 구성 요소를 xml 레이아웃에 다음과 같이 추가하여 테스트 해보십시오.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <here.is.my.package.Custom android:id="@+id/add" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     initValue="2" maxValue="7" stepSize="1" /> 
</LinearLayout> 


이것은 작동하지 않으며 기본값 (0, 1, 5) 만 표시됩니다. 나는 뭔가를 놓치고 있습니까? 아니면 정상적인 행동입니까?

답변

8

좋아요, 제 질문에 대한 답변을 찾았습니다. 대답은 단순히 네임 스페이스가없는 사용자 정의 xml 특성을 사용하고 안드로이드가이를 무시하고 기본값을 제공한다는 것입니다. 내 네임 스페이스를 추가 한 후 :

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:customAttribute="http://schemas.android.com/apk/res/gere.is.my.package" 
     android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <here.is.my.package.Custom android:id="@+id/add" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      customAttribute:initValue="2" customAttribute:maxValue="7" customAttribute:stepSize="1" /> 
    </LinearLayout> 

모든 것이 작동했습니다. Gradle을 프로젝트에서

+0

친애하는 동일한 문제가 나를 위해 ... CustomTextView 만든 및 seprate xml 레이아웃을 만들 수 있으므로 어느 누구도 그냥 사용할 수 있습니다 ...하지만 사용자 지정 값 특성을 가져 오려고했는데 .. 기본 제공 가치 만 ... 모든 제안. – CoDe

+0

@Shubh 문제의 모든 세부 사항을 제공하는 새로운 질문을 올리십시오. – Luksprog

+0

그것은 나를 위해 일하고 .... 다시 작업 공간을 다시 다음과 같은 프로젝트 정리 않았다 ... 그리고 그것은 일을 시작 ... 감사합니다 Logged – CoDe

1

의 xmlns를 사용있는 CustomView = "http://schemas.android.com/apk/res-auto"

이 나를 위해 작동합니다!

관련 문제