|  | 
 
| 本帖最后由 liu 于 2018-12-3 20:31 编辑 include
 
 
  
 
 平时开发中,会遇到有部分布局多次出现,我们为了避免重复写这些布局就可以使用include,这样也能提高代码的复用性。
 
 使用方法:新建一个要多次使用的布局,然后在要使用的界面内对应位置添加 <include layout="@layout/要引用的布局名字" />即可
 
 示例代码:
 
 activity_main.xml:
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/layout_title" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/account"
                android:textColor="@android:color/black"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/editText_account"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:background="@android:color/transparent"
                android:hint="@string/sign_account"
                android:textSize="20sp" />
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorPrimaryDark" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/password"
                android:textColor="@android:color/black"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/editText_password"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:background="@android:color/transparent"
                android:hint="@string/sign_password"
                android:textSize="20sp" />
        </LinearLayout>
        <TextView
            android:id="@+id/tv_login"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/bg_login"
            android:gravity="center"
            android:text="@string/login"
            android:textColor="@android:color/white"
            android:textSize="20sp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="end"
            android:text="@string/forget_password"
            android:textColor="@android:color/black"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>layout_title.xml:
 
 [XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/holo_blue_bright">
    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:src="@drawable/icon_back" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/login"
        android:textColor="@android:color/white"
        android:textSize="23sp" />
</RelativeLayout>效果如下图所示:
 
 
   
 
 
 
 | 
 |