Android 中 include的使用

include就是在一个布局中,导入另一个布局

include 可以使相同的页面就写一次, 提高了共同布局的复用性

先定义一个共通布局

8053f69fcd3f3735c14114c989446f06025.jpg

布局里先随便写一个按钮控件

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

    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello,Include"
        
        />
</LinearLayout>

然后再想要引入布局的布局文件中添加include

    <!--引入布局文件-->
    <include layout="@layout/include_layout"/>

如下

<?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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--引入布局文件-->
    <include layout="@layout/include_layout"/>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/wx_lanyu/article/details/84835230