Android LayoutInflater分析

来源:互联网 发布:qq飞升坐骑升阶数据 编辑:程序博客网 时间:2024/06/14 11:47

相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的。而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的任务通常都是在Activity中调用setContentView()方法来完成的。其实setContentView()方法的内部也是使用LayoutInflater来加载布局的。
先来看一下LayoutInflater的基本用法吧,它的用法非常简单,首先需要获取到LayoutInflater的实例,有三种方法可以获取到:

LayoutInflater inflater = LayoutInflater.from(context);
LayoutInflater inflater = context.getLayoutInflater();

当然,还有另外一种写法也可以完成同样的效果:

LayoutInflater inflater = (LayoutInflater) context          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

其实第一二种就是第三种的简单写法,只是Android给我们做了一下封装而已。得到了LayoutInflater的实例之后就可以调用它的inflate()方法来加载布局了,如下所示:

layoutInflater.inflate(int resource, ViewGroup root);
layoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot);

inflate()方法可以接受两个或三个参数,第一个参数就是要加载的布局resId,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null,第三个参数指是否把resId这个view加入到了parent。

  • 两个参数方法

先调用 layoutInflater.inflate(resId, root); 这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。
下面我们就通过一个例子,来更加直观地看一下LayoutInflater的用法:

public class MainActivity extends Activity {     private LinearLayout mainLayout;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          mainLayout = (LinearLayout) findViewById(R.id.main_layout);          LayoutInflater layoutInflater = LayoutInflater.from(this);          View view= layoutInflater.inflate(R.layout.button_layout, null);          mainLayout.addView(view);      }    }  

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/main_layout"      android:layout_width="match_parent"      android:layout_height="match_parent"    android:orientation="vertical"></LinearLayout>

button_layout.xml

<Button  xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"      android:layout_height="match_parent" >        android:text="LayoutInflater" /> 

这里写图片描述
Button在界面上显示出来了!说明我们确实是借助LayoutInflater成功将button_layout这个布局添加到LinearLayout中了。LayoutInflater技术广泛应用于需要动态添加View的时候。其实LayoutInflater就是使用Android提供的pull解析方式来解析布局文件的。

<Button xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="300dp"      android:layout_height="80dp"      android:text="LayoutInflater" >    </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。
再来看一下我们的button_layout.xml吧,很明显Button这个控件目前不存在于任何布局当中,所以layout_width和layout_height这两个属性理所当然没有任何作用。那么怎样修改才能让按钮的大小改变呢?解决方法其实有很多种,最简单的方式就是在Button的外面再嵌套一层布局,如下所示:

<RelativeLayout 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="80dp"          android:text="LayoutInflater" >      </Button>    </RelativeLayout>

可以看到,这里我们又加入了一个RelativeLayout,此时的Button存在与RelativeLayout之中,layout_width和layout_height属性也就有作用了。当然,处于最外层的RelativeLayout,它的layout_width和layout_height则会失去作用。现在重新运行一下程序
这里写图片描述

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

public class MainActivity extends Activity {     private LinearLayout mainLayout;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          mainLayout = (LinearLayout) findViewById(R.id.main_layout);          ViewParent viewParent = mainLayout.getParent();          Log.d("TAG", "the parent of mainLayout is " + viewParent);      }    }  

运行后打印的Log如下:
the parent of mainLayout is android.widght.FrameLayout@41069ff8

非常正确!LinearLayout的父布局确实是一个FrameLayout,而这个FrameLayout就是由系统自动帮我们添加上的。
说到这里,虽然setContentView()方法大家都会用,但实际上Android界面显示的原理要比我们所看到的东西复杂得多。任何一个Activity中显示的界面其实主要都由两部分组成,标题栏和内容布局。标题栏就是在很多界面顶部显示的那部分内容,比如刚刚我们的那个例子当中就有标题栏,可以在代码中控制让它是否显示。而内容布局就是一个FrameLayout,这个布局的id叫作content,我们调用setContentView()方法时所传入的布局其实就是放到这个FrameLayout中的,这也是为什么这个方法名叫作setContentView(),而不是叫setView()。

  • 三个参数方法
    (temp代表用resId创建的view)
    • Inflate(resId,null)
      只创建temp,返回temp,而此temp的getLayoutParams为null
    • Inflate(resId,root,false)
      创建temp,然后执行temp.setLayoutParams(params); 这个params正是root.generateLayoutParams(attrs);得到的,返回temp
    • Inflate(resId , root, true )
      创建temp,然后执行root.addView(temp, params); 最后返回root。它不仅能够正确的处理,而且已经把resId这个view加入到了root,并且返回的是root,和以上两者返回值有绝对区别

接下来我们来验证一下以上结论:

<Button xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:id="@+id/id_btn"      android:layout_width="120dp"      android:layout_height="120dp"      android:text="LayoutInflater" >  </Button> 
public class MainActivity extends Activity {      private LayoutInflater mInflater;        @Override      protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         mInflater = LayoutInflater.from(this);           View view1 = mInflater.inflate(R.layout.activity_main, null);         View view2 = mInflater.inflate(R.layout.activity_main,(ViewGroup)findViewById(android.R.id.content), false);         View view3 = mInflater.inflate(R.layout.activity_main,(ViewGroup)findViewById(android.R.id.content), true);          Log.e("TAG", "view1 = " + view1  +" , view1.layoutParams = " + view1.getLayoutParams());          Log.e("TAG", "view2 = " + view2  +" , view2.layoutParams = " + view2.getLayoutParams());          Log.e("TAG", "view3 = " + view3);       }   }

注:parent我们用的是Activity的内容区域:即android.R.id.content,是一个FrameLayout,我们在setContentView(resId)时,其实系统会自动为其包上一层FrameLayout(id=content)。
按照我们上面的说法:
view1的layoutParams 应该为null
view2的layoutParams 应该不为null,且为FrameLayout.LayoutParams
view3为FrameLayout,且将这个button添加到Activity的内容区域了(因为R.id.content代表Actvity内容区域)

下面看一下输出Log:
view1=android.widget.Button@429d1660,view1.layoutParams=null
view2=android.widget.Button@42a0e120,view2.layoutParams=android.widget.FrameLayout$LayoutParams@42a0e9a0
view3=android.widget.FrameLayout@42a0a240
完全正确!

这里写图片描述

虽然我们没有执行setContentView,但是依然可以看到绘制的控件,是因为

View view3 = mInflater.inflate(R.layout.activity_main,(ViewGroup)findViewById(android.R.id.content), true);

这个方法内部已经执行了root.addView(temp , params); 上面已经解析过了。

最后再附上一张Activity窗口的组成图吧,以便于大家更加直观地理解:
这里写图片描述
一般我们自己写Activity时会把系统的标题栏隐藏,然后自定义自己需要样式的标题栏,隐藏系统标题栏方法:

requestWindowFeature(Window.FEATURE_NO_TITLE);   
<style name="Theme.Translucent.NoTitleBar">     <item name="android:windowNoTitle">true</item></style>
0 0
原创粉丝点击