Android studio APP开发 小tip 如何重用布局文件layout?

重用布局文件

在一个布局文件里面引用其他已经写好的布局文件,用 指令。
代码如下:

<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>

界面效果如图:
引用界面效果

发布了23 篇原创文章 · 获赞 3 · 访问量 4642

猜你喜欢

转载自blog.csdn.net/Ace_bb/article/details/104045602