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. <?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>
22. <?xml version="1.0"
23. encoding="utf-8"?>
24. <LinearLayout
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. <TextView
31. android:layout_width="fill_parent"
32. android:layout_height="wrap_content"
33. android:text="@string/hello"
34. />
35. <Button
36. android:id="@+id/button"
37. android:layout_width="wrap_content"
38. android:layout_height="wrap_content"
39. android:text="ShowCustomDialog"
40. />
41. </LinearLayout>
附2:
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>