Android LayoutInflater原理分析,带你一步步深刻了解View(一) 郭霖学习摘要

public class MainActivity extends Activity {
    //-----------------------UI----------------
    private RelativeLayout mMainLayout;
    private LinearLayout mBtnContainerLayout;
    //--------------------初始化----------------
    private int width;
    private int height;
    private int btnWidth;
    private int btnHeight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMainLayout = (RelativeLayout)findViewById(R.id.layout_main);
        Log.d("TAG", "the parent layout is :" + mMainLayout.getParent());
        
        //LayoutInflater 动态载入button
        LayoutInflater inflater = LayoutInflater.from(this);
        View button  = inflater.inflate(R.layout.layout_button,null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(600,300);
        button.setLayoutParams(params);
        mMainLayout.addView(button);     
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
   
</RelativeLayout>

inflate()方法还有个接收三个参数的方法重载,结构以下:html

[java] view plaincopy在CODE上查看代码片派生到个人代码片java

  1. inflate(int resource, ViewGroup root, boolean attachToRoot)  android

那么这第三个参数attachToRoot又是什么意思呢?其实若是你仔细去阅读上面的源码应该能够本身分析出答案,这里我先将结论说一下吧,感兴趣的朋友能够再阅读一下源码,校验个人结论是否正确。ide

1. 若是root为null,attachToRoot将失去做用,设置任何值都没有意义。布局

2. 若是root不为null,attachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。this

3. 若是root不为null,attachToRoot设为false,则root参数失去做用。spa

4. 在不设置attachToRoot参数的状况下,若是root不为null,attachToRoot参数默认为true。.net

好了,如今对LayoutInflater的工做原理和流程也搞清楚了,你该知足了吧。额。。。。还嫌这个例子中的按钮看起来有点小,想要调大一些?那简单的呀,修改layout_button.xml中的代码,以下所示:code

[html] view plaincopy在CODE上查看代码片派生到个人代码片orm

  1. <Button xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="300dp"  
        android:layout_height="80dp"  
        android:text="Button" >  
      
    </Button>

这里咱们将按钮的宽度改为300dp,高度改为80dp,这样够大了吧?如今从新运行一下程序来观察效果。咦?怎么按钮仍是原来的大小,没有任何变化!是否是按钮仍然不够大,再改大一点呢?仍是没有用!

其实这里无论你将Button的layout_width和layout_height的值修改为多少,都不会有任何效果的,由于这两个值如今已经彻底失去了做用。平时咱们常常使用layout_width和layout_height来设置View的大小,而且一直都能正常工做,就好像这两个属性确实是用于设置View的大小的。而实际上则否则,它们实际上是用于设置View在布局中的大小的,也就是说,首先View必须存在于一个布局中,以后若是将layout_width设置成match_parent表示让View的宽度填充满布局,若是设置成wrap_content表示让View的宽度恰好能够包含其内容,若是设置成具体的数值则View的宽度会变成相应的数值。这也是为何这两个属性叫做layout_width和layout_height,而不是width和height。

再来看一下咱们的layout_button.xml吧,很明显Button这个控件目前不存在于任何布局当中,因此layout_width和layout_height这两个属性理所固然没有任何做用。那么怎样修改才能让按钮的大小改变呢?解决方法其实有不少种,最简单的方式就是在Button的外面再嵌套一层布局,以下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="300dp"
        android:layout_height="90dp"
        android:text="button" />
</LinearLayout>

能够看到,这里咱们又加入了一个LinearLayout,此时的Button存在与LinearLayout之中,layout_width和layout_height属性也就有做用了。固然,处于最外层的LinearLayout,它的layout_width和layout_height则会失去做用。如今从新运行一下程序,结果以下图所示:

                      

OK!按钮的终于能够变大了,这下总算是知足你们的要求了吧。

看到这里,也许有些朋友心中会有一个巨大的疑惑。不对呀!平时在Activity中指定布局文件的时候,最外层的那个布局是能够指定大小的呀,layout_width和layout_height都是有做用的。确实这主要是由于,在setContentView()方法中,Android会自动在布局文件的最外层再嵌套一个FrameLayout,因此layout_width和layout_height属性才会有效果。那么咱们来证明一下吧,修改MainActivity中的代码,如顶部MainActivity代码中的所示: 

Log.d("TAG", "the parent layout is :" + mMainLayout.getParent());


能够看到,这里经过findViewById()方法,拿到了activity_main布局中最外层的LinearLayout对象,而后调用它的getParent()方法获取它的父布局,再经过Log打印出来。如今从新运行一下程序,结果以下图所示:

 

很是正确!LinearLayout的父布局确实是一个FrameLayout,而这个FrameLayout就是由系统自动帮咱们添加上的。

说到这里,虽然setContentView()方法你们都会用,但实际上Android界面显示的原理要比咱们所看到的东西复杂得多。任何一个Activity中显示的界面其实主要都由两部分组成,标题栏和内容布局。标题栏就是在不少界面顶部显示的那部份内容,好比刚刚咱们的那个例子当中就有标题栏,能够在代码中控制让它是否显示。而内容布局就是一个FrameLayout,这个布局的id叫做content,咱们调用setContentView()方法时所传入的布局其实就是放到这个FrameLayout中的,这也是为何这个方法名叫做setContentView(),而不是叫setView()。

最后再附上一张Activity窗口的组成图吧,以便于你们更加直观地理解:

            

相关文章
相关标签/搜索