LayoutInflater 类似于findViewById 只不过前者的对象是布局,后者的对象是控件,这是我的理解。
而且findViewById是View的方法,也就是说使用它必须是在要找控件的父控件下面才可以。这时候通常是用LayoutInlater的实例的findViewById方法
下面给出个代码实例:
package com.example.newadt.zhuqi.first; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends Activity { private Button btn_click; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_click = (Button)findViewById(R.id.btn_click); btn_click.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showCustomDialog(); } private void showCustomDialog() { AlertDialog.Builder builder; // AlertDialog alertDialog; //对话框 Context mContext = MainActivity.this; //下面三种方法都可以 ////LayoutInflater inflater = getLayoutInflater(); // LayoutInflater inflater = LayoutInflater.from(MainActivity.this); LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog,null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello, Welcome to Mr Wei's blog!"); Log.i("smallpig", "the text is "+text.getText()); ImageView image = (ImageView) layout.findViewById(R.id.image); //注意:此处findViewById的方法使用者是layout。不加会报错 image.setImageResource(R.drawable.ic_launcher); builder = new AlertDialog.Builder(mContext); builder.setView(layout); //把view加入对话框 alertDialog = builder.create();//生成对话框 alertDialog.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
布局文件为
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv" android:text="@string/clickme" /> </RelativeLayout>
对话框custom_dialog的布局文件:
<?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="horizontal" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:background="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" android:text="@string/clickme" /> </LinearLayout>