Android LayoutInflater(布局扩展)总结

修炼-------------Android LayoutInflater(布局扩展)总结

在实际开发中LayoutInflater这个类还是非常实用的,根据LayoutInflater字母翻译为布局扩展。这个类有点类似于ActivityfindViewById(int id)这个方法,只不过findViewById这个方法查找的是UI组件,ButtonTextView,

LayoutInflater则是实例化xml文件,将一个xml文件通过器其inflate方法转换为一个View,以便于其他组件实用.AlertDialog.BuildersetView方法等

LayoutInflater的主要作用的就是:inflate

参照SDK文档:

This class 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对象的方法主要有:

1.通过getLayoutInflater()获得LayoutInflater inflater = getLayoutInflater();

2.通过LayoutInflater.from(Context context)获得

3.通过getSystemService(String str)获得

下面通过演示一个图示来简要解释下LayoutInflater,如图一

 

当用户单击"弹出一个自定义对话框"按钮时,则如图二

源码:

主要代码如下(获取LayoutInflater)

//得到LayoutInflater,以下几种方式都可以

LayoutInflater inflater = null;

//inflater = getLayoutInflater();

//inflater = LayoutInflater.from(LayoutInflaterTestActivity.this);

inflater=(LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

//实例化一个xml为View

View view = inflater.inflate(R.layout.custom_dialognull);

转载内容:

http://blog.163.com/guozioo@126/blog/static/64086947201052925641679/

3. LayoutInflater.inflate() 

Layout文件转换为View,顾名思义,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById(),这一段描述有误,看如下代码 。看下面文档写的已经很清楚。

更多详情请参照SDK文档

Xml代码 

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

<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。 

4. findViewById2中形式 

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

1Activity中的findViewById() 

public View findViewById (int id) 

Finds a view that was identified by the id attribute from the XML that was processed in 

onCreate(Bundle)

ReturnsThe view if found or null otherwise. 

2View中的findViewById() 

public final View findViewById (int id) 

Look for a child view with the given id. If this view has the given id, return this view.

Parameters       id  The id to search for.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值