使用LayoutInflater类方法来实现。
主要用到下面两个方法:
【1】、public static LayoutInflater from(Context context)
从指定的容器中创建LayoutInflater 对象。一般指定的容器就是当前MainActivity。所以参数就是MainActivity.this。
【2】、public View inflate(int resource, ViewGroup root)
从布局文件的定义转化成View对象。
举例说明:一个简单地用户登录定制界面。
login.xml文件源代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:text="用户名:"
android:layout_marginLeft="20dp"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"