标签:布局 layout android layoutinflater
//1,通过系统服务获取布局加载器 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(resource,root,attachToRoot); //2,通过activity中的getLayoutInflater()方法 View view = getLayoutInflater().inflate(resource,root,attachToRoot); //3,通过View的静态inflate()方法 View view = View.inflate(resource,root,attachToRoot); //4,通过LayoutInflater的inflate()方法 View view = LayoutInflater.from(this).inflate(resource,root,attachToRoot);
public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); } public View inflate(int resource, ViewGroup root, boolean attachToRoot) { if (DEBUG) System.out.println("INFLATING from resource: " + resource); XmlResourceParser parser = getContext().getResources().getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); } }
if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // 如果root不等于null,获取它的LayoutParams params = root.generateLayoutParams(attrs); if (!attachToRoot) { //attachToRoot等于false,把root的LayoutParams属性给temp temp.setLayoutParams(params); } }
//attachToRoot等于true,将temp加入到root这个viewGroup中 if (root != null && attachToRoot) { root.addView(temp, params); } // root等于null,attachToRoot等于false,直接把temp赋值给返回结果 if (root == null || !attachToRoot) { result = temp; }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MyActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="120dp" android:layout_height="120dp" android:background="@color/blue"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello inflate" android:textColor="@android:color/white" /> </LinearLayout>
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = getLayoutInflater().inflate(R.layout.view, null, false); setContentView(view); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = getLayoutInflater().inflate(R.layout.view, null, true); setContentView(view); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rootView = getLayoutInflater().inflate(R.layout.activity_my, null); view = getLayoutInflater().inflate(R.layout.view,(ViewGroup)rootView,false); setContentView(view); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rootView = getLayoutInflater().inflate(R.layout.activity_my, null); view = getLayoutInflater().inflate(R.layout.view,(ViewGroup)rootView,true); setContentView(view); }
这里因为root的布局为RelativeLayout,我们把view加入到root中,view本身保留了自有的LayoutParm
最后我要吐槽,为什么csdn不支持markdown,我都是先用Mou写好的,贴过来竟然要自己重新排版。。。
标签:布局 layout android layoutinflater
原文地址:http://blog.csdn.net/kennethyo/article/details/40429031