资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android的LayoutInflater-创新互联

在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如 Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

publicabstractclass LayoutInflater extends Object

获得 LayoutInflater 实例的三种方式

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);

其实,这三种方式本质是相同的,从源码中可以看出:这三种方式最终本质是都是调用的Context.getSystemService()。

下面是一个Demo

成都创新互联公司从2013年成立,先为唐县等服务建站,唐县等地企业,进行企业商务咨询服务。为唐县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
  1. main.xml

  2. encoding="utf-8"?>

  3. xmlns:android="http://schemas.android.com/apk/res/android"

  4.   android:orientation="vertical"

  5.   android:layout_width="fill_parent"

  6.   android:layout_height="fill_parent"

  7.   >

  8.   android:layout_width="fill_parent"

  9.   android:layout_height="wrap_content"

  10.   android:text="@string/hello"

  11.   />

  12.   android:id="@+id/button"

  13.   android:layout_width="wrap_content"

  14.   android:layout_height="wrap_content"

  15.   android:text="ShowCustomDialog"

  16.   />

复制代码

定义对话框的布局方式custom_dialog.xml

  1. encoding="utf-8"?>

  2. xmlns:android="http://schemas.android.com/apk/res/android"

  3.           android:orientation="horizontal"

  4.           android:layout_width="fill_parent"

  5.           android:layout_height="fill_parent"

  6.           android:padding="10dp"

  7.           >

  8.   

  9.           android:layout_width="wrap_content"

  10.           android:layout_height="fill_parent"

  11.           android:layout_marginRight="10dp"

  12.           />

  13.   

  14.           android:layout_width="wrap_content"

  15.           android:layout_height="fill_parent"

  16.           android:textColor="#FFF"

  17.           />

复制代码

Activity代码

  1. package com.android.tutor;

  2. import android.app.Activity;

  3. import android.app.AlertDialog;

  4. import android.content.Context;

  5. import android.os.Bundle;

  6. import android.view.LayoutInflater;

  7. import android.view.View;

  8. import android.view.View.OnClickListener;

  9. import android.widget.Button;

  10. import android.widget.ImageView;

  11. import android.widget.TextView;

  12. public class LayoutInflaterDemo extends Activity implements

  13. OnClickListener {

  14. private Button button;

  15.   public void onCreate(Bundle savedInstanceState) {

  16.       super.onCreate(savedInstanceState);

  17.       setContentView(R.layout.main);

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

  19.       button.setOnClickListener(this);

  20.   }

  21. @Override

  22. public void onClick(View v) {

  23.   showCustomDialog();

  24. }

  25. public void showCustomDialog()

  26. {

  27.   AlertDialog.Builder builder;

  28.   AlertDialog alertDialog;

  29.   Context mContext = LayoutInflaterDemo.this;

  30.   //下面俩种方法都可以

  31.   //LayoutInflater inflater = getLayoutInflater();

  32.   LayoutInflater inflater = (LayoutInflater)  mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

  33.   View layout = inflater.inflate(R.layout.custom_dialog,null);  //返回值为view

  34.   TextView text = (TextView) layout.findViewById(R.id.text);

  35.   text.setText("Hello, Welcome to Mr Wei's blog!");

  36.   ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath);

  37.   p_w_picpath.setImageResource(R.drawable.icon);

  38.   builder = new AlertDialog.Builder(mContext);

  39.   builder.setView(layout);

  40.   alertDialog = builder.create();

  41.   alertDialog.show();

  42. }

  43. }

复制代码

运行效果:

Android的LayoutInflater

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:Android的LayoutInflater-创新互联
本文路径:http://cdkjz.cn/article/hjedc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220