2010-12-14 11 views
7

मैं एंड्रॉइड पर एक कस्टम View बनाना चाहता हूं। मैंने इसे यथासंभव सरल करने की कोशिश की है और लगभग खाली कक्षा MyView बनाई है और इसे अपने LinearLayout में उपयोग किया है लेकिन एप्लिकेशन "फोर्स क्लोज़" से शुरू होने में विफल रहता है। मैं एक साधारण कस्टम View कैसे कर सकता हूं? Building Custom Components के अनुसार View आकार 100x100 प्राप्त करता है यदि मैं onMeasure() ओवरराइड नहीं करता हूं।एक साधारण कस्टम व्यू कैसे बनाएं?

public class MyView extends View { 

    public MyView(Context context) { 
     super(context); 
    } 
} 

और मैं एक LinearLayout में उपयोग के साथ:

<view 
    class="com.example.MyView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" /> 

क्या मैं गलत कर रहा हूँ?


अगर मैं निर्माता है कि सुझाव है itemon और सुपर क्लास के लिए इसी कॉल का उपयोग करें। फिर "फोर्स क्लोज़" चला गया है, लेकिन मेरा LinearLayout टूटा हुआ है, MyView के बाद के घटक नहीं दिखाए गए हैं।

यहाँ मेरी main.xml है:

public MyView(Context context, AttributeSet attrs) 

एंड्रॉयड ढांचे ऊपर निर्माता से अपने दृश्य के साथ यूआई का निर्माण करने की कोशिश करेंगे:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
    android:background="#f00" 
    android:text="Hello" 
/> 
<view 
    class="com.example.MyView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.0" 
    android:background="#00f" 
    android:text="World" 
/> 
</LinearLayout> 
+0

आप यहां एक अच्छे नमूने से परामर्श कर सकते हैं: [http://www.sgoliver.net/blog/?p=1457 ](http://www.sgoliver.net/blog/?p=1457) –

+0

मुझे समान जरूरत है .. क्या आपको वह चाहिए जो आपको चाहिए। कुछ कोड कृपया देखें – Nepster

उत्तर

9

आप इस तरह एक और निर्माता विधि निर्धारित कर सकते हैं हो सकता है ।

+0

धन्यवाद, यह "फोर्स क्लोज़" संदेश को हटा देता है, लेकिन मेरा 'लीनियरलाउट' टूटा हुआ है, इसके बाद घटक नहीं दिखाया गया है। – Jonas

+2

मुझे 'Measure() 'को ओवरराइड करना पड़ा। अब यह बहुत अच्छा काम करता है। – Jonas

9

एंड्रॉइड डेवलपर गाइड में बिल्डिंग कस्टम घटक नामक एक अनुभाग है। दुर्भाग्यवश, एक्सएमएल विशेषताओं की चर्चा केवल लेआउट फ़ाइल के अंदर नियंत्रण घोषित करने और वर्ग प्रारंभिकरण के अंदर मूल्यों को संभालने में शामिल नहीं है। attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyCustomView"> 
     <attr name="android:text"/> 
     <attr name="android:textColor"/>    
     <attr name="extraInformation" format="string" /> 
    </declare-styleable> 
</resources> 

सूचना घोषणा-Styleable टैग में एक अयोग्य नाम का इस्तेमाल

प्रचार मूल्यों में जिम्मेदार बताते हैं \: चरणों इस प्रकार हैं। गैर-मानक एंड्रॉइड विशेषताओं जैसे अतिरिक्त जानकारी को उनके प्रकार घोषित करने की आवश्यकता है। सुपरक्लास में घोषित टैग पुनर्विक्रय किए बिना उप-वर्गों में उपलब्ध होंगे।

कंस्ट्रक्टर्स

के बाद से वहाँ दो कंस्ट्रक्टर्स कि initialisation के लिए एक AttributeSet का उपयोग कर रहे हैं बनाने के लिए, यह कंस्ट्रक्टर कॉल करने के लिए के लिए एक अलग initialisation विधि बनाने के लिए सुविधाजनक है।

private void init(AttributeSet attrs){ 
    TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView); 
    //Use a 
    Log.i("test",a.getString(R.styleable.MyCustomView_android_text)); 
    Log.i("test",""+a.getColor(R.styleable.MyCustomView_android_textColor, Color.BLACK)); 
    Log.i("test",a.getString(R.styleable.MyCustomView_android_extraInformation)); 
    //Don't forget this 
    a.recycle(); 
} 

R.styleable.MyCustomView एक स्वत: जनरेट की int [] संसाधन प्रत्येक तत्व का एक विशेषता की आईडी है। तत्व नाम पर विशेषता नाम जोड़कर एक्सएमएल में प्रत्येक प्रॉपर्टी के लिए विशेषताएं उत्पन्न की जाती हैं। विभिन्न कार्यों को प्राप्त करने के जरिए गुणों को टाइप किए गए ऐरे से पुनर्प्राप्त किया जा सकता है। यदि एक्सएमएल में विशेषता परिभाषित नहीं है, तो शून्य वापस आ गया है। बेशक, अगर वापसी का प्रकार एक आदिम है, तो इस मामले में दूसरा तर्क वापस आ गया है।

यदि आप सभी विशेषताओं को पुनर्प्राप्त नहीं करना चाहते हैं, तो यह सरणी मैन्युअल रूप से बनाना संभव है। मानक एंड्रॉइड विशेषताओं के लिए आईडी एंड्रॉइड.आर.एटरआर में शामिल है, जबकि इस परियोजना के लिए गुण आर में हैं। attr।

int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor}; 

कृपया ध्यान दें कि आपको एंड्रॉइड.R.styleable में कुछ भी उपयोग नहीं करना चाहिए, इस धागे के अनुसार यह भविष्य में बदल सकता है। यह अभी भी दस्तावेज में है क्योंकि इन सभी स्थिरांकों को एक ही स्थान पर देखना उपयोगी है।

इस तरह के लेआउट \ main.xml के रूप में एक लेआउट फाइलों में इसका इस्तेमाल नाम स्थान घोषणा शामिल

xmlns: एप्लिकेशन = "http://schemas.android.com/apk/res/com.mycompany .projectname "

शीर्ष स्तर xml तत्व में।

<com.mycompany.projectname.MyCustomView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:text="Test text" 
    android:textColor="#FFFFFF" 
app:extraInformation="My extra information"; 
/> 

पूरी तरह से योग्य नाम का उपयोग कर कस्टम दृश्य का संदर्भ लें।

एंड्रॉयड LabelView नमूना

आप एक पूरा उदाहरण चाहते हैं, एंड्रॉयड लेबल दृश्य नमूना को देखो।

LabelView.java

TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); 
CharSequences=a.getString(R.styleable.LabelView_text); 
attrs.xml 

<declare-styleable name="LabelView"> 
    <attr name="text"format="string"/> 
    <attr name="textColor"format="color"/> 
    <attr name="textSize"format="dimension"/> 
</declare-styleable> 

custom_view_1.xml

<com.example.android.apis.view.LabelView 
    android:background="@drawable/blue" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    app:text="Blue"app:textSize="20dp"/> 

यह एक नाम स्थान विशेषता के साथ एक LinearLayout में निहित है:

xmlns: एप्लिकेशन = "http: // schemas.android.com/apk/res/com.example.android.apis "

संबंधित मुद्दे