http://weizhulin.blog.51cto.com/1556324/311450/
大家好我们这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
下面我将详细的说明Demo的实现过程:
1、新建一个 Android工程,我们命名为LayoutInflaterDemo.
2、修改main.xml布局,里面主要在原来基础上增加了一个Button.代码如下:
1. view plaincopy to clipboardprint?
2. <?xml version="1.0"
3. encoding="utf-8"?>
4. <LinearLayout
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. <TextView
11. android:layout_width="fill_parent"
12. android:layout_height="wrap_content"
13. android:text="@string/hello"
14. />
15. <Button
16. android:id="@+id/button"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:text="ShowCustomDialog"
20. />
21. </LinearLayout>
3.定义对话框的布局方式,我们在layout目录下,新建一个名为 custom_dialog.xml文件具体代码如下:
1. view plaincopy to clipboardprint?
2. <?xml version="1.0"
3. encoding="utf-8"?>
4. <LinearLayout
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. <ImageView android:id="@+id/image"
12. android:layout_width="wrap_content"
13. android:layout_height="fill_parent"
14. android:layout_marginRight="10dp"
15. />
16. <TextView android:id="@+id/text"
17. android:layout_width="wrap_content"
18. android:layout_height="fill_parent"
19. android:textColor="#FFF"
20. />
21. </LinearLayout>
22.
4.修改主程序LayouInflaterDemo.java代码如下:
1. view plaincopy to clipboardprint?
2. package com.android.tutor;
3. import android.app.Activity;
4. import android.app.AlertDialog;
5. import android.content.Context;
6. import android.os.Bundle;
7. import android.view.LayoutInflater;
8. import android.view.View;
9. import android.view.View.OnClickListener;
10. import android.widget.Button;
11. import android.widget.ImageView;
12. import android.widget.TextView;
13. public class LayoutInflaterDemo extends Activity implements
14. OnClickListener {
15.
16. private Button button;
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.main);
20.
21. button = (Button)findViewById(R.id.button);
22. button.setOnClickListener(this);
23. }
24. @Override
25. public void onClick(View v) {
26.
27. showCustomDialog();
28. }
29.
30. public void showCustomDialog()
31. {
32. AlertDialog.Builder builder;
33. AlertDialog alertDialog;
34. Context mContext = LayoutInflaterDemo.this;
35.
36. //下面俩种方法都可以
37. LayoutInflater inflater = getLayoutInflater();
38. LayoutInflater inflater = (LayoutInflater)
39. mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
40. View layout = inflater.inflate(R.layout.custom_dialog,null);
41. TextView text = (TextView) layout.findViewById(R.id.text);
42. text.setText("Hello, Welcome to Mr Wei's blog!");
43. ImageView image = (ImageView) layout.findViewById(R.id.image);
44. image.setImageResource(R.drawable.icon);
45. builder = new AlertDialog.Builder(mContext);
46. builder.setView(layout);
47. alertDialog = builder.create();
48. alertDialog.show();
49. }
50. }
51.
52. 5、最后执行之,点击Button,将得到上述效果。
LayoutInflater的作用:
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
方法:
Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面.
其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml,
下面是一个简单的例子:
首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
为了让大家容易理解我[转]做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。