LayoutInflater是用来找layout下xml布局文件,并且实例化!
而findViewById()是找具体View下的某个(如:Button,TextView等)【控件已经实实例化了】。
为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,
而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
下面我将详细的说明Demo的实现过程:
1、新建一个 Android工程,我们命名为LayoutInflaterDemo.
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.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class LayoutInflaterDemo extends Activity implements
OnClickListener {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
showCustomDialog();
}
public void showCustomDialog()
{
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = LayoutInflaterDemo.this;
//下面俩种方法都可以
//LayoutInflater inflater = getLayoutInflater();
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!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
附1:
main.xml布局文件:
1. view plaincopy to clipboardprint?
2. 3. encoding="utf-8"?>
4. 5. xmlns:android="http://schemas.android.com/apk/res/android"
6. android:orientation="vertical"
7. android:layout_width="fill_parent"
8. android:layout_height="fill_parent"
9. >
10. 11. android:layout_width="fill_parent"
12. android:layout_height="wrap_content"
13. android:text="@string/hello"
14. />
15. 16. android:id="@+id/button"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:text="ShowCustomDialog"
20. />
21.
22. 23. encoding="utf-8"?>
24. 25. xmlns:android="http://schemas.android.com/apk/res/android"
26. android:orientation="vertical"
27. android:layout_width="fill_parent"
28. android:layout_height="fill_parent"
29. >
30. 31. android:layout_width="fill_parent"
32. android:layout_height="wrap_content"
33. android:text="@string/hello"
34. />
35. 36. android:id="@+id/button"
37. android:layout_width="wrap_content"
38. android:layout_height="wrap_content"
39. android:text="ShowCustomDialog"
40. />
41.
附2:
custom_dialog.xml文件具体代码如下:
1. view plaincopy to clipboardprint?
2. 3. encoding="utf-8"?>
4. 5. xmlns:android="http://schemas.android.com/apk/res/android"
6. android:orientation="horizontal"
7. android:layout_width="fill_parent"
8. android:layout_height="fill_parent"
9. android:padding="10dp"
10. >
11. 12. android:layout_width="wrap_content"
13. android:layout_height="fill_parent"
14. android:layout_marginRight="10dp"
15. />
16. 17. android:layout_width="wrap_content"
18. android:layout_height="fill_parent"
19. android:textColor="FFF"
20. />
21.