android:layout下面创建子目录

一,创建common目录

然后在common目录下创建layout目录

二,原来在layout目录下的xml需要另放一个目录

我们创建一个main目录,main目录下创建layout,移动到这里

三,在build.gradle中添加已创建目录:

   //在layout文件夹下建立子文件夹
    sourceSets {
        main{
            res.srcDirs = [
                    'src/main/res/layout/common',
                    'src/main/res/layout/main',
                    'src/main/res/layout',
                    'src/main/res'
            ]
        }
    }

如图:

四,实际使用

代码:

common/layout目录下title_bar.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="50dp"
    android:id="@+id/bar"
    android:gravity="center"
    android:background="@android:color/holo_blue_light">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题栏"
        android:textSize="30dp"
        android:textColor="@android:color/white"
    />
</LinearLayout>

实际include

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.Scroll2Activity">


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:id="@+id/houseNestedScrollView"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:fillViewport="true"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/title_bar"/>

            <Button
                android:id="@+id/buttonGo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="跳转"
                />

说明:不需要添加子目录名,这些子目录只起到让结构清晰的作用

测试效果:

posted @ 2025-06-02 12:27  刘宏缔的架构森林  阅读(77)  评论(0)    收藏  举报