2011-09-19 6 views
5

काम नहीं कर रहा है मैं कस्टम घटकों के बारे में सीख रहा हूं और मुझे कस्टम एक्सएमएल विशेषताओं के साथ कुछ परेशानी हो रही है।
मेरा कस्टम घटक LinearLayout और कन्स्ट्रक्टर (public Custom(Context context, AttributeSet attrs)) में विस्तार करता है, मैं एक एक्सएमएल लेआउट (2 बटन और 1 एडिटटेक्स्ट) को बढ़ा रहा हूं।
मैं भी values/attrs में घोषित इस कस्टम विशेषताओं:
टाइपेडएरे

<?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 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 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> 

सब कुछ काम किया।

+0

प्रिय मेरे लिए समस्या दिखा रहा है ... मैंने एक कस्टमटेक्स्ट व्यू बनाया है और एक सीप्रेट एक्सएमएल लेआउट बनाया है, इसलिए कोई भी इसे उपयोग करके उपयोग कर सकता है ... लेकिन जब मैंने कस्टम वैल्यू एट्रिब्यूट लाने की कोशिश की .. यह डिफ़ॉल्ट दे रहा है मूल्य केवल ... कोई सुझाव। – CoDe

+0

@ शुभ कृपया समस्या का सभी विवरण प्रदान करने वाला एक नया प्रश्न पोस्ट करें। – Luksprog

+0

यह मेरे लिए काम कर रहा है .... फिर से कार्यक्षेत्र को पुनरारंभ करने के बाद प्रोजेक्ट क्लीनअप किया ... और यह काम करना शुरू कर दिया ... धन्यवाद – CoDe

1

Gradle परियोजनाओं में,

xmlns का उपयोग करें: customView = "http://schemas.android.com/apk/res-auto"

यह मेरे लिए काम करता है!