2011-11-08 6 views
6

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

  • आवश्यकता: कस्टम व्यू (xml लेआउट फ़ाइल का उपयोग करके) बनाने की आवश्यकता है ताकि एक ही दृश्य को मेरी एप्लिकेशन गतिविधियों में उपयोग किया जाना चाहिए। यहाँ मैं नमूना कोड जो मैं पर काम कर रहा हूँ के साथ जाना,

cutomviewxml.xml

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

विस्तारित देखें वर्ग ... कोड ...

mycustomTextview.java

public class mycustomTextview extends View { 

    private View mView; 
    Context mycontext; 

    public mycustomTextview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 

    this.mycontext = context; 

    LayoutInflater inflater; 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mView = inflater.inflate(R.layout.cutomviewxml, null); 
    } 

गतिविधि main.xml फ़ाइल

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


    <com.motorola.mycustomTextview 
     android:id="@+id/customtextview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_marginLeft="143dp" 
     android:layout_marginTop="69dp" 
     android:layout_toRightOf="@+id/textView1" /> 

</RelativeLayout> 

गतिविधि वर्ग sample.java ..

public class sample extends Activity{ 

    private static final String LOG_TAG = "sampleActivity"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.i(LOG_TAG,"OnCreate of sampleActivity"); 
     setContentView(R.layout.main); 
} 
} 

उत्तर

5

मुझे लगता है कि आप mycustomTextview, में एक छोटी सी गलती किया है जब आप अपने लेआउट बढ़ आप ViewGroup भी पारित करने के लिए है, तो आप इस लाइन

mView = inflater.inflate(R.layout.cutomviewxml, this); 
3

उपयोग इस अपने xml में:

<?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"> 
     <yourpackagename.viewclassname android:id="@+id/myView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></yourpackagename.viewclassname> 
</LinearLayout> 
+0

+1 उपयोग कर सकते हैं। धन्यवाद, यह वास्तव में मदद की। मैं इस पर पागल हो रहा था :) –

4

कस्टम के अंदर विचारों तक पहुंचने के लिए FinishInflate() विधि पर ओवरराइड देखें और findViewById() का उपयोग करें। हाथ में समस्या के लिए समाधान निम्नानुसार होगा।

public class mycustomTextview extends LinearLayout { 

    Context mycontext; 
    private TextView textView1; 

    public mycustomTextview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.mycontext = context; 
    } 

    @Override 
    protected void onFinishInflate() { 
     textView1=(TextView) findViewById(R.id.textView1); 
    } 
} 
0
public class ExampleView extends FrameLayout { 

     ImageView mImage; 

     public ExampleView(Context context) { 
      super(context); 
      init(context); 
     } 

     public void init(Context context) { 
      View view = inflate(context, R.layout.my_view_layout,this); 
      view.findViewById(R.id.image) 
     } 
} 
संबंधित मुद्दे