LayoutInflater的使用效果和findViewById()有点类似,不过它有更多的功能,具体可以参考api;
代码:
}else if(v.getId()==R.id.dialog_Button){ LayoutInflater inflater = (LayoutInflater) MyGridActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.main_item, null); EditText txt = (EditText) layout.findViewById(R.id.dia_txt); txt.setText("Dialog"); Button btn =(Button) layout.findViewById(R.id.dia_btn); ImageView img = (ImageView) layout.findViewById(R.id.dia_image); img.setImageResource(R.drawable.super_btn_png1); Builder builder = new AlertDialog.Builder(MyGridActivity.this); builder.setView(layout); AlertDialog dialog = builder.create(); dialog.show(); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub /** * * */ } });
dialog_xml:
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" > <ImageView android:id="@+id/dia_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <Button android:id="@+id/dia_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/dia_image" android:layout_marginRight="40dp" android:layout_marginTop="131dp" android:layout_toLeftOf="@+id/dia_image" android:text="Button" /> <EditText android:id="@+id/dia_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" > <requestFocus /> </EditText> </RelativeLayout>
效果: