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. <?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>

复制代码

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

  1. <?xml version="1.0"

  2. encoding="utf-8"?>

  3. <LinearLayout

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

  5.           android:orientation="horizontal"

  6.           android:layout_width="fill_parent"

  7.           android:layout_height="fill_parent"

  8.           android:padding="10dp"

  9.           >

  10.   <ImageView android:id="@+id/p_w_picpath"

  11.           android:layout_width="wrap_content"

  12.           android:layout_height="fill_parent"

  13.           android:layout_marginRight="10dp"

  14.           />

  15.   <TextView android:id="@+id/text"

  16.           android:layout_width="wrap_content"

  17.           android:layout_height="fill_parent"

  18.           android:textColor="#FFF"

  19.           />

  20. </LinearLayout>

复制代码

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-创新互联
文章路径:https://www.cdcxhl.com/article12/hjedc.html

成都网站建设公司_创新互联,为您提供微信小程序网站制作商城网站App设计面包屑导航移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设