Android中LayoutInflater的用法

Android中LayoutInflater实例
2010年09月16日 星期四 上午 10:01

LayoutInflater与findViewById的用法有很多可比较之处。

如:他们都可以通过id返回View。

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_dialog,null);

Button button = (Button) findViewById(R.id.button);

不同之处是:LayoutInflater是用来实例化整个布局文件,而findViewById()是实例化布局文中中的View。

下面是一个实例。 主 布局main.xml里有一个TextView和一个Button,当点击Button,出现Dialog,而这个Dialog的布局方式是我们在 layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

源代码如下:

main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ShowCustomDialog"/> </LinearLayout>

custom_dialog.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF"/> </LinearLayout>

TestLayoutInflater.java:

package com.android.test;

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 TestLayoutInflater 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 = TestSlidingDrawer.this;

     //Three ways are OK.
     LayoutInflater inflater = getLayoutInflater();

     Activity.getLayoutInflater() or Window.getLayoutInflater().
     LayoutInflater inflater = LayoutInflater.from(this);
     Obtains the LayoutInflater from the given context.
     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, This is a LayoutInflater Demo");
     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();
     Using AlertDialog directly.
     alertDialog = new AlertDialog(mContext) {//};
     alertDialog.setView(layout);
     alertDialog.show();
}
}

几点说明:

  1. LayoutInflater的获取有三种方法:Activity.getLayoutInflater(), LayoutInflater.from(Context), getSystemService(String).
  2. 也可以不通过AlertDialog的内部类 AlertDialog.Builder来获取AlertDialog对象,AlertDialog也能setView(View).另外,注 意:AlertDialog的构造函数有些特殊,需要加{},如果去掉就报“The constructor AlertDialog(Context) is not visible”的错误。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值