• Android中的LayoutInflater和inflate


    1、inflate方法

    inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。

    android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别:

           如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layout上的其他组件的内容,你就必须用inflate()方法先将对话框的layout找出来,然后再用findViewById()找到它上面的其它组件。例如:

    1. View view1=View.inflate(this,R.layout.dialog_layout,null);  
    2.     
    3.   TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
    4.     
    5.   dialogTV.setText("abcd");  

    注:R.id.dialog_tv是在对话框layout上的组件,而这时若直接用this.findViewById(R.id.dialog_tv)肯定会报错。

    [html] view plaincopy
     
    1. View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  

    Inflate()或可理解为“隐性膨胀”,隐性摆放在view里,inflate()前只是获得控件,但没有大小没有在View里占据空间,inflate()后有一定大小,只是出于隐藏状态。

    2、LayoutInflater类  实例讲解

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

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

    LayoutInflater 是一个抽象类,在文档中如下声明:public abstract class LayoutInflater extends Object

    package cn.csdn.activity;
    
    import android.app.TabActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.widget.TabHost;
    
    public class TabHostActivity extends TabActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            TabHost tabhost = this.getTabHost();
    
            /**
             * LayoutInflater这个类的作用类似于findViewById(),
             * 不同点:
             *     LayoutInflater是用来找layout下xml布局文件的,而且它会实例化
             *     findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮
             * 
             * 
             * 
             * inflate就相当于将一个xml中定义的布局找出来.    
             * 因为如果在一个Activity文件里直接用findViewById()这个方法的话,
             * 那么它所对应的是setConentView()中调用的那个layout里的组件.   
             * 因此如果在同样的Activity里用到别的layout的话,
             *     而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容,
             *     那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件
             *     然后进行一系列的操作
             *     
             *     inflate()方法中参数:
             *       1.想要用的布局文件的id
             *       2.持有选项卡的内容,获取FrameLayout
             *       3.true:将此处解析的xml文件做为根视图View
             */
            LayoutInflater.from(this).inflate(R.layout.tabhost_layout,
                    tabhost.getTabContentView(), true);
    
            
            /**在这里添加的时候:
             *       1.必须指定 tab 的内容,必须为 id, 即:setContent(R.id.text)
             *       2.必须设置tab 上的文字或图片  , 即:setIndicator("已接电话")
             *       3.返回一个 TabHost.TabSpec 对象,其参数用于标识一个 tab 的 tag,即:newTabSpec("tab1")
            */
            tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已接电话")
                    .setContent(R.id.text));
            
            tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("呼出电话",
                    getResources().getDrawable(R.drawable.ic_launcher))
                    .setContent(R.id.text));
            
            tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("未接电话")
                    .setContent(R.id.text));
        }
    }
  • 相关阅读:
    msp430入门编程25
    msp430入门编程24
    msp430入门编程23
    msp430入门编程22
    msp430入门编程21
    msp430入门编程20
    msp430入门编程16
    msp430入门编程15
    msp430入门编程14
    msp430入门编程13
  • 原文地址:https://www.cnblogs.com/wshuang/p/4871552.html
Copyright © 2020-2023  润新知