2014-07-16 2 views
0

나는 사용자 정의 환경을 개발하기 시작했다 :안드로이드 사용자 지정 리소스 식별자는

<com.package.lib.android.preference.CustomPreference 
    xmlns:customPreference="http://schemas.android.com/apk/src/com.package.lib.android.preference.CustomPreference" 
    android:key="test" 
    android:title="test" 
    customPreference:minValue="4" /> 

연관된 클래스 : 나는 customPreference:minValue에 액세스하려면 내 CustomPreference 클래스에서

public class CustomPreference extends DialogPreference { 
    private Context mContext; 
    private LayoutInflater mLayoutInflater; 

    public CustomPreference(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 

     mContext = context; 
     mLayoutInflater = LayoutInflater.from(mContext); 

     // how can I access customPreference:minValue? 
    } 
} 

. 어떻게 가능합니까?

+0

을 당신은 단지를 사용할 수 없습니다 'AttributeSet' attrs? http://developer.android.com/reference/android/util/AttributeSet.html#getAttributeIntValue(java.lang.String,%20java.lang.String,%20int) – Blacklight

+0

죄송합니다. 전에 AttributeSet을 사용하지 않았습니다. 그래서 나는 오류가 어디에 있는지 당신에게 말할 수는 없지만 나의 이해로부터 거기에 있어야한다. 중단 점을 설정하고 디버그하고'AttributeSet'에 무엇이 있는지 확인하십시오. – Blacklight

+0

나도 마찬가지입니다. 저는'minValue'의 값을'@ integer/default_value'로 변경하여 작동하도록했습니다. 'attrs.getAttributeIntValue ("vntnumberpickerpreference", "minValue", 0)'여전히 나를 0으로 만듭니다. – Niklas

답변

1

나는 그것을 작동시켰다.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:customPreference="http://schemas.android.com/apk/res-auto" > 

    <com.package.lib.android.preference.CustomPreference 
     android:defaultValue="@integer/font_size_default_value" 
     android:key="test" 
     android:title="title" 
     customPreference:minValue="@integer/font_size_min_value" 
     customPreference:maxValue="@integer/font_size_max_value" /> 

은 다음과 같은 내용으로 res/values 폴더에 attrs.xml 파일을 만듭니다

환경 설정 클래스에서
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <declare-styleable name="CustomPreference"> 
     <attr name="minValue" format="integer" /> 
     <attr name="maxValue" format="integer" /> 
    </declare-styleable> 

</resources> 

사용 속성을 얻을 수 있습니다 :

final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomPreference); 

Log.i(TAG, String.valueOf(a.getInt(R.styleable.CustomPreference_minValue, 0))); 
Log.i(TAG, String.valueOf(a.getInt(R.styleable.CustomPreference_maxValue, 100))); 

a.recycle();