2010-09-13 7 views
27

को AttributeSet पारित करने के लिए मैं कैसे एक कस्टम दृश्य वर्ग के लिए वर्तमान AttributeSet पारित करते हैं? अगर मैं ऐसे कन्स्ट्रक्टर का उपयोग करता हूं जिसमें केवल तर्कों में संदर्भ है, तो मैं उस कस्टम व्यू के लिए xml में "शैली" टैग का उपयोग करने की सभी थीम और क्षमता खो देता हूं।कैसे कस्टम देखें

मैंने क्या किया गया है प्रोग्राम के रूप में एक गतिविधि xml फ़ाइल में पहले से ही अपने कस्टम दृश्य जिसमें बनाते हैं और फिर एक नया बना और लेआउट में जोड़ने के लिए है। मुझे लगता है कि एक्सएमएल में बनाया गया एक उचित स्टाइल है, और जिसे मैं प्रोग्रामेटिक रूप से बनाता हूं वह नहीं करता है।

दोनों के बीच अंतर जहाँ तक मैं बता सकता है कि प्रणाली CustomLayout1(Context context, AttributeSet attrs) निर्माता का उपयोग करता है। समस्या यह है कि जब मैं इसे प्रोग्रामेटिक रूप से बनाता हूं तो इस कस्टम व्यू को पास करने के लिए एप्लिकेशन के लिए AttributeSet को कैसे प्राप्त किया जाए, यह पता नहीं लगा सकता है।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/mainlayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<com.clearsync.test.theme1.CustomLayout1 android:id="@+id/maincustom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

कस्टम दृश्य वर्ग:

import com.clearsync.test.theme1.R; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.widget.LinearLayout; 

public class CustomLayout1 extends LinearLayout { 
private Context context = null; 

public CustomLayout1(Context context) { 
    super(context); 
    this.context = context; 
    create(); 
} 

public CustomLayout1(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    create(); 
} 

private void create(){ 
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.inflateme, this, true); 
} 
} 

और अंत में, कस्टम दृश्य xml:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 

public class ThemeOne extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout); 

     layout.addView(new CustomLayout1(getApplicationContext())); 
    } 
} 

यहां मुख्य एक्सएमएल है:

यहाँ गतिविधि है

<?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"> 
<TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Oh, Hewroh..." 
    style="?textview_style1" /> 
</LinearLayout> 

उत्तर

27

इसके बजाय layout.addView (नई CustomLayout1 (getApplicationContext())) के साथ निर्माण की; अपनी गतिविधि में लेआउट इन्फ्लेटर के साथ इसे फुलाएं।

LayoutInflater inflater = LayoutInflater.from(this); 
inflater.inflate(R.layout.yourcustomviewxml, layout); 
+1

सरल बनाएं। सुरुचिपूर्ण। धन्यवाद। मुझे अपने बारे में सोचना चाहिए था :- पी यह एक बहुत ही कष्टप्रद व्यवहार है जिसे मैं निश्चित रूप से देखना पसंद करूंगा। मैंने अपने मुख्य कोड में उस समाधान को लागू किया है, और निश्चित रूप से कई अन्य संबंधित मुद्दों को उजागर किया है। ऐसा लगता है कि एक बग की तरह लगता है कि थीम और स्टाइल बस इतना ही रहस्यमय तरीके से गायब हो जाएगा क्योंकि जिस तरह से आप कस्टम व्यू कहते हैं। * श्वास * – atraudes

+0

[कस्टम दृश्य की प्रतिलिपि बनाने के लिए लेआउट इन्फ्लैटर का उपयोग करने का एक पूर्ण उदाहरण यहां दिया गया है।] (http://stackoverflow.com/a/41500409/3681880) – Suragch

2

आप यहां क्या करने की कोशिश कर रहे हैं? ऐसा लगता है कि आपके निर्माण विधि का उपयोग करके यहां एक अंतहीन रिकर्सिव लूप है, क्योंकि inflate() कन्स्ट्रक्टर को कॉल करेगा जो गुण लेता है। वैसे भी आपके प्रश्न का उत्तर देने के लिए आपको गुणों के साथ निर्माता में गुण मिलते हैं!

निर्माता है कि जब एक्सएमएल से लोड हो रहा है कहा जाता है यही कारण है कि, अन्यथा यह अन्य निर्माताओं है कि आप की आपूर्ति में से एक कहता है।

एक अन्य उपयोगी बात है, आप स्थिर देखें विधि से बहुत आसान inflater के लिए एक संदर्भ प्राप्त कर सकते हैं। View.inflate: डी

+0

लिए यह एक परीक्षण वर्ग मैं एक समस्या मैं एक अन्य परियोजना में हो रही है हल करने के लिए बनाया गया था। मैं कस्टम विचार बना रहा हूं और उन्हें गैलरी दृश्य में जोड़ रहा हूं। मेरी समस्या यह है कि मैं उस थीम को लागू नहीं कर सकता जिसका उपयोग मैं कस्टम क्लास के अंदर किसी भी चीज़ पर कर रहा हूं। यह विकृत और गलत लगता है: -P मेरा प्रश्न यह है कि मैं अपनी कस्टम कक्षा में जाने के लिए गतिविधि में विशेषता सेट कैसे प्राप्त करूं? मैं वर्तमान में इस कर रहा हूँ: नई CustomLayout1 (getApplicationContext()) और मैं इस के बजाय की तरह कुछ करना चाहते हैं: नई CustomLayout1 (getApplicationContext(), AttributeSet) उम्मीद है कि यह वास्तव में लागू होगा मेरी थीम – atraudes

+0

और view.inflate पर आपकी टिप के लिए धन्यवाद।बहुत उपयोगी :- डी – atraudes

+0

आप उन्हें कन्स्ट्रक्टर में ले जाते हैं, बस एक एट्रिब्यूटसेट सदस्य mAttset = attrs – schwiz

3

आपका कोड आपके कस्टम व्यू के लिए रैखिक लेआउट के अंदर LinearLayout बनाता है। ऐसा करने का सही तरीका से अपने कस्टम दृश्य एक्सएमएल बदल रहा है:

<?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"> 
<TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Oh, Hewroh..." 
    style="?textview_style1" /> 
</LinearLayout> 

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
<TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Oh, Hewroh..." 
     style="?textview_style1" 
/> 
</merge> 
+0

http://developer.android.com पर संदर्भ जोड़ना /training/improving-layouts/reusing-layouts.html और http://stackoverflow.com/questions/2732682/simple-example-of-merge-and-include-usage-in-android-xml-layouts – EpicPandaForce

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