2013-10-24 5 views
1

업데이트 - 마지막에 해결 방법을 참조하십시오. 이제 설명이 필요합니다. 나는 시도하고 속성을 검색 할 때 내가 의미있는 값을 얻을 수 내가 같은 질문문제를 검색하는 데 문제가 있습니다

를 찾을 수 없습니다 검색에도 불구하고 나는 사용자 정의 Viewpager를 생성하고 뷰 호출기 생성자에서 attrs.xml이

에서 사용자 지정 속성을 정의 . 나는 사용했다.

abstract int  getAttributeIntValue(String namespace, String attribute, int defaultValue) 

'attribute'의 정수 값을 반환한다. 당신이 볼 수 있듯이

public MyViewPager(Context context, AttributeSet attrs) { 
    Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeUnsignedIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9)); 
    Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto","view_pager_type",9)); 
    Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","view_pager_type")); 
    Log.d(Constant.TAG, "ViewPagerType:" + attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","garbage")); 

는 attrs.getAttributeValue 문자열을했지만 그것은 나에게 의미가 없다. 이 로그 문에서 출력

-9가 발견되지 않는 경우는 기본이고, 나는 @ 213 번호가 무엇인지 undertand에없는

ViewPagerType:9   
ViewPagerType:9   
ViewPagerType:@2131230723 
ViewPagerType:null  

이 내 attrs.xml이있다

<resources> 
    <declare-styleable name="AutoResizeTextView"> 
     <attr name="ttf_name" format="string"/> 
    </declare-styleable> 
    <declare-styleable name="ViewPagerType"> 
     <attr name="view_pager_type" format="integer"/> 
    </declare-styleable> 
</resources> 

이것은이 integer.xml

내 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<org.rh.ellierides.MyViewPager xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/textPager" 
    app:view_pager_type="@integer/text_viewpager" 
    android:orientation="horizontal" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="24" 
    tools:context=".Main"> 

입니다

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <integer name="text_viewpager">1</integer> 
    <integer name="image_viewpager">2</integer> 
</resources> 

나는 두 개의 개별 뷰어를 만들 것이라고 생각하지만 여전히 이것을 이해하고 싶습니다.


업데이트 나는 해결책을 찾았지만 첫 번째는 그래서 여전히

TypedArray a = null; 
    try { 
     a = getContext().obtainStyledAttributes(attrs, R.styleable.ViewPagerType); 
     int viewpagerType = a.getInt(R.styleable.ViewPagerType_view_pager_type, 9); 
     Log.d(Constant.TAG, "viewpagerType:" + viewpagerType); 
    } finally { 
     if (a != null) { 
      a.recycle(); // ensure this is always called 
     } 
    } 

답변

0

다음 작품에 답하십시오 작동하지 않는 이유를 이해하지 않습니다하지만 위에서 말했듯이 나에게 그것을 설명하십시오. 생성자에게 전달되는 attrs는 무엇입니까?

TypedArray a = null; 
    try { 
     a = getContext().obtainStyledAttributes(attrs, R.styleable.ViewPagerType); 
     int viewpagerType = a.getInt(R.styleable.ViewPagerType_view_pager_type, 9); 
     Log.d(Constant.TAG, "viewpagerType:" + viewpagerType); 
    } finally { 
     if (a != null) { 
      a.recycle(); // ensure this is always called 
     } 
    } 
관련 문제