项目中有用到过这个方法,看到别人的讲解很详细,做下笔记,以便以后用到 在Linearlayout的构造函数中通过使用映射机制加载布局文件,即通过Inflater方法,在使用该方法自定义的控件时,由于所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立的控件形式在Layout文件中声明: [java] view plain copy public class CustomLayout extends LinearLayout{ public CustomLayout(Context context){ LayoutInflater mInflater = LayoutInflater.from(context); View myView = mInflater.inflate(R.layout.receive, null); addView(myView); } } 布局文件 [java] view plain copy < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" androidundefinedrientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" androidundefinedrientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> < /LinearLayout>