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
    评论
Android StudioLayoutInflater是一个用于将XML布局文件转换为对应的View对象的类。它可以帮助我们在代码动态地创建和操纵UI元素。 在Android开发,我们通常会使用XML布局文件来定义应用程序的界面。而LayoutInflater就是用来解析这些XML布局文件并将其转换为View对象的工具。 通过LayoutInflater,我们可以使用代码动态地加载布局文件,并将其转换为可操作的View对象,以便进一步进行各种操作,比如修改View的属性、添加事件监听器等。 我们可以通过以下步骤来使用LayoutInflater: 1. 首先,在Activity或Fragment获取LayoutInflater的实例,可以使用`getLayoutInflater()`方法或者`LayoutInflater.from(context)`来获取。 2. 然后,使用LayoutInflater的`inflate()`方法将XML布局文件转换为View对象。该方法接受三个参数:要解析的布局文件的资源ID、要将布局文件解析成的父ViewGroup,以及是否将生成的View添加到父ViewGroup。 3. 最后,我们可以对生成的View对象进行进一步操作,比如设置属性、添加事件监听器等。 这样,我们就可以在代码动态地创建和操纵UI元素了。LayoutInflater的使用可以帮助我们实现更灵活和动态的界面设计。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Android高手进阶教程(五)之----Android LayoutInflater的使用!](https://blog.csdn.net/Android_Tutor/article/details/5513869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值