`
xiehongdong
  • 浏览: 68633 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android - LayoutInflater 的使用

阅读更多
LayoutInflater is used to instantiate layout XML file into its corresponding View objects. It is never be used directly -- use getLayoutInflater()or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on.
也就是说我们用LayoutInflater做一件事:inflate。inflate这个方法总共有四种形式,目的都是把xml表述的layout转化为View。
For example:
LayoutInflater inflater = (LayoutInflater)>context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 

1. Context.public abstract Object getSystemService (String name) :Return the handle to a system-level service by name. The class of the returned object varies by the requested name

 

 


 2. 有2种获得LayoutInflater的方法

(1)通过SystemService获得 LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

(2)从给定的contex获取 protected LayoutInflater (Context context)

(3)二者区别:实质是一样的,

  请看源码 

public static LayoutInflater from(Context context) { LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (LayoutInflater == null) { throw new AssertionError("LayoutInflater not found."); } return LayoutInflater; }



  

 


 3. findViewById有2中形式

R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate方法),R.id.xx是引用布局文件里面的组件,组件的id是xx...(findViewById方法)。看看R.java配置文件吧,R对文件分类管理,多写几个layout.xml后你会发现,所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常

(1)Activity中的findViewById()


 
(2)View中的findViewById()


  
3. LayoutInflater.inflate()
将Layout文件转换为View,顾名思义,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作只能通过Inflater,而不能通过findViewById(),这一段描述有误,看如下代码看下面文档写的已经很清楚
---------------------------------------------------------------------------------------------
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/placeslist_linearlayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> </ScrollView>




  LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout);   linearLayout.addView(place_type_text); 

 这是可运行的,这上面的xml中,LinearLayout不再是Layout的代表,而只是一个普通的View。 
  • 大小: 11.1 KB
  • 大小: 57 KB
  • 大小: 22.3 KB
分享到:
评论

相关推荐

    down-test-Android 获得 LayoutInflater 实例的三种方式

    down-test-Android 获得 LayoutInflater 实例的三种方式

    Android 中LayoutInflater的使用

    在Android开发中,LayoutInflater是一个非常关键的工具,它主要用于将XML布局文件转换...理解并熟练掌握LayoutInflater的使用,对于开发高效且灵活的Android应用至关重要。它可以极大地提高代码的可维护性和用户体验。

    android-query

    首先,我们来看如何在Android应用中使用Android-query框架。基础的使用方式是创建一个AQuery对象,通常会与一个视图或Fragment关联。例如: ```java AQuery aq = new AQuery(view); ``` 一旦创建了AQuery实例,你...

    Android LayoutInflater的用法

    在Android开发中,`LayoutInflater`是一个非常重要的工具类,它主要用于将XML布局文件转换为视图对象,使得我们可以动态地将界面元素添加到应用程序中。`LayoutInflater`是Android框架的一部分,它极大地增强了UI...

    Android 中级应用 一 LayoutInflater 的使用

    在Android应用程序中,我们通常使用XML来定义用户界面的布局,而`LayoutInflater`则起到了桥梁的作用,将静态的XML布局文件转换成可交互的UI组件。 `LayoutInflater`的基本用法包括以下步骤: 1. **获取实例**:...

    android中LayoutInflater的使用.pdf

    在Android开发中,`LayoutInflater`是一个非常关键的工具类,主要用于将XML布局文件转换为视图对象(View)。它的核心作用在于动态加载和实例化UI布局,使得开发者可以在运行时根据需要创建和修改用户界面。在本文中...

    Aj_04的Android 中LayoutInflater的使用(源码)

    测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:&lt;activity android:name="LayoutInflaterDemo"&gt;&lt;/activity&gt;

    android oschina-android-app源码.rar

    可能会使用SQLite数据库存储用户偏好或应用数据,或者使用Room Persistence Library,这是Android提供的一个更高级的数据库抽象层。同时,文件存储、SharedPreferences也可能被用于简单数据的保存。 4. **网络请求...

    android-support-v7-recyclerview.jar

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder ...

    Android--RadioGroupFragment结合使用.rar

    在这个名为"Android--RadioGroupFragment结合使用.rar"的压缩包中,我们可能找到了一个示例项目,它演示了如何将RadioGroup与Fragment巧妙地结合起来,以创建更加复杂且灵活的UI交互。 首先,RadioGroup是Android中...

    Android高手进阶教程与Android基础教程

    Android高手进阶教程之----Android 中LayoutInflater的使用! .doc Android高手进阶教程之----Android 中MenuInflater的使用(布局定义菜单)! .doc Android高手进阶教程之----Android 中Preferences的使用! .doc ...

    Android 中LayoutInflater(布局加载器)之实战篇Demo

    Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....

    android-swipelistview Demo 例子使用

    本示例将详细讲解如何在项目中使用`android-swipelistview`库来实现这种效果。 首先,我们需要了解`SwipeListView`的基本概念。它是对Android原生`ListView`的一个扩展,增加了左右滑动的功能,通常用于创建具有...

    Android--开发--PopupWindow下拉列表.rar

    - 使用LayoutInflater加载布局,并将其设置为PopupWindow的内容视图。 - 设置PopupWindow的宽度和高度,一般宽度设为MATCH_PARENT,高度可以是WRAP_CONTENT或根据需要设定的具体值。 - 设置PopupWindow是否能获得...

    android换肤包lib

    这个lib包是专为Android Studio设计的,可以直接导入到项目中使用,简化了换肤功能的实现过程。 一、Android换肤原理 Android换肤主要是通过替换资源文件来实现的。每个主题或皮肤都是一组特定的资源(如颜色、图片...

    Android LayoutInflater加载布局详解及实例代码

    在Android应用开发中,我们通常使用LayoutInflater来动态地加载和插入布局,这在创建自定义视图、处理动态数据或者在运行时创建视图时非常有用。本文将深入解析LayoutInflater的工作原理,并提供实例代码来帮助理解...

    Android LayoutInflater深入分析及应用

    arser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Do nothing } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!...

    Android-Android开发使用PupopWindow在指定View的上下左右动态显示菜单列表

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_menu, null); PopupWindow popupWindow = new PopupWindow...

    Android-使用RecyclerView实现的自定义课程表View

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_item, parent, false); return new CourseViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull ...

Global site tag (gtag.js) - Google Analytics