from:
http://www.cnblogs.com/tt_mc/archive/2010/05/28/1746307.html
http://blog.csdn.net/notice520/article/details/6667827
custom component :
1. make an custom layout xml file as a template .
2. Inflate the xml file to get the layout instence .
3. Must use the cuntom_view.findViewById(int id) to find the views inside it.
4. two types of the inflate function;
a. inflate the cunstom layout xml to the empty view I instances . attachToRoot must be true;
//method 1 match R.layout.custom_dialog to view; LinearLayout view=new LinearLayout(context); inflater.inflate(R.layout.custom_dialog,view,true);
b. return the view that get by inflate function
//method 2 , return the view that has been match; View view=inflater.inflate(R.layout.custom_dialog, null);
custom_dialog.xml:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_margin="10dip" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#ffffff" /> </LinearLayout>
package com.layoutInflater_test; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class LayoutInflaterTest01 extends Activity { private Button inflateButton; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inflateButton=(Button)this.findViewById(R.id.inflateButton); inflateButton.setOnClickListener(inflateListener); } private OnClickListener inflateListener=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); showCustomDialog(); } }; private void showCustomDialog(){ AlertDialog.Builder builder; AlertDialog alertDialog; Context context=LayoutInflaterTest01.this; LayoutInflater inflater= (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE); //method 1 match R.layout.custom_dialog to view; LinearLayout view=new LinearLayout(context); inflater.inflate(R.layout.custom_dialog,view,true); //method 2 , return the view that has been match; // View view=inflater.inflate(R.layout.custom_dialog, null); LinearLayout mainLayout=(LinearLayout)inflater.inflate(R.layout.main,null); // id only can be found by the directly root view ImageView img=(ImageView)view.findViewById(R.id.image); // ImageView img=(ImageView)LayoutInflaterTest01.this.findViewById(R.id.image); // fail // ImageView img=(ImageView)mainLayout.findViewById(R.id.image); // fail img.setImageResource(R.drawable.pdf_icon); TextView text=(TextView)view.findViewById(R.id.text); // ImageView img=(ImageView)LayoutInflaterTest01.this.findViewById(R.id.text); //fail // TextView text=(TextView)mainLayout.findViewById(R.id.text); //fail text.setText("inflater testing"); builder=new AlertDialog.Builder(context); builder.setView(view); alertDialog=builder.create(); alertDialog.show(); // LayoutInflaterTest01.this.setContentView(view); } }