android 开发LayoutInflater之我见

来源:互联网 发布:csgo枪支数据 编辑:程序博客网 时间:2024/05/01 07:46

  以前写项目的时候经常会用到 public View inflate(int resource, ViewGroup root, boolean attachToRoot) 和 public View inflate(XmlPullParser parser, ViewGroup root),以前使用的时候,没有遇到什么问题就没有深入了解。后来,由于特殊的要求,就研究了一下。下面来讲解一下他们两者的区别、联系和作用。

项目中我有一个.xml find_text_unit的布局文件如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:background="@drawable/find_text_bg"    android:gravity="center_horizontal"    android:orientation="vertical" >    <TextView        android:id="@+id/title_label"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingRight="5dp"        android:paddingTop="5dp"        android:text="题材0"        android:textColor="@color/category_orange"        android:textSize="13sp"        android:visibility="visible" />    <TextView        android:id="@+id/count_label"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingBottom="5dp"        android:paddingLeft="5dp"        android:paddingRight="5dp"        android:text="0"        android:textSize="12sp" /></LinearLayout>

可以从上面的.xml布局中 我们可以看到我最外层的LinearLayout有

    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"

然后我在一个自定义的一个类里去加载了这个布局文件

public class FindTextView extends LinearLayout{private Context context;
public FindTextView(Context context) {super(context);this.context = context;reloadView();}public FindTextView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;reloadView();}public void reloadView(){View textView = LayoutInflater.from(context).inflate(R.layout.find_text_unit, this,false);addView(textView);}}
可以看到我用的是LayoutInflater.from(context).inflate(R.layout.find_text_unit,this,false),当textView被加到parentView的时候,我们的

android:layout_marginLeft="10dp"android:layout_marginRight="10dp"

这两句起了作用。 

当然使用LayoutInflater.from(context).inflate(R.layout.find_text_unit,this,true),也会起作用。相当于这前面两句。

 View textView = LayoutInflater.from(context).inflate(R.layout.find_text_unit, this,false); addView(textView);

但如果我们能使用

 View textView = LayoutInflater.from(context).inflate(R.layout.find_text_unit, null,false); addView(textView);

这两句

android:layout_marginLeft="10dp"android:layout_marginRight="10dp"
加到view上时,.xml里的layout_marginLeft和right并没有起任何作用都是默认的0;


我们可以写成

          View textView = LayoutInflater.from(context).inflate(R.layout.find_text_unit,null,false);
  LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  textLayoutParams.setMargins(ScreenUtils.dip2px(context, 10), 0, ScreenUtils.dip2px(context, 10), 0);
  textView.setLayoutParams(textLayoutParams);
  addView(textView);

即自己设定从.xml解析出来的view的LayoutParams。然后加到view上。


说了上面的效果,我们来看看这个函数,

 public View inflate(int resource, ViewGroup root, boolean attachToRoot) ;

Parameters:
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy (ifattachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (ifattachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
Returns:
The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.
根据文档我们可以看出,

如果 public View inflate(int resource, ViewGroup root, boolean attachToRoot) 里

attachToRoot传的是true,那么这个函数返回的结果就是root,就是把从.xml解析的view加到rootView之后,返回的rootview。但不可root为null,attachToRoot 为true。

如果为false,则返回从.xml解析出来的view,root的作用仅仅就是确定这个view的LayoutParams,如果此时root也为null,则返回的从.xml解析出来的view 在.xml定义的LayoutParams参数不会有作用。  就比如我们上面的那个改为

View textView = LayoutInflater.from(context).inflate(R.layout.find_text_unit, null,false);addView(textView);
则find_text_unit里的

  android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"

不会有任何作用。


讲了上面的我想大家就会明白public View inflate(int resource, ViewGroup root)的用法了,这个就是 public View inflate(int resource, ViewGroup root, boolean attachToRoot) 的特殊情况,它的实现如下:

public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }


0 0
原创粉丝点击