重用布局文件
在一个布局文件里面引用其他已经写好的布局文件,用 指令。
代码如下:
<include layout="@layout/activity_splash"/>
下面是预先创建号的activity_include.xml文件, 这个布局文件将在另一个布局文件中被引用。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/text_color_second"
android:gravity="center">
<TextView
android:id="@+id/splash_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APP启动界面"
android:textColor="@color/text_background_1"
android:background="@color/text_color_first"
android:textSize="@dimen/splash_textsize"/>
</LinearLayout>
界面如图:
下面这个布局文件activity_include.xml将引用上面创建的这个activity_splash.xml文件。使用开头说的代码:
全部代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/activity_splash"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这个界面将引用其他布局文件"
android:textColor="@color/include_text"
android:textSize="30sp"/>
</LinearLayout>
界面效果如图: