如何在 Android Studio 中的多个活动/片段中重复使用相同的 xml 背景布局



我正在开发一个Android应用程序,该应用程序几乎在每个活动上都具有相同的背景图像和底部导航栏。

我想重用这段代码,而不是每次都写出来:

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background_image">

    <android.support.design.widget.BottomNavigationView
        android:layout_gravity="bottom"
        android:id="@+id/bottom_nav_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@drawable/bottom_nav_menu_image"
    xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>

这将是所有视图的基本背景;但是我将在每个视图的顶部放置不同的布局。 是否可以使用插入和合并标签以这种方式启动每个新活动?

需要下面的xml文件调用

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

这是我的 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/MyDarkToolbarStyle">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:gravity="end"
                android:orientation="horizontal"
                android:weightSum="10">

                <!--<ImageView-->
                <!--android:id="@+id/img_back"-->
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:src="@drawable/ic_back" />-->
                <TextView
                    android:id="@+id/txt_header"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:layout_weight="7"
                    android:gravity="left"
                    android:text="Design Collaboration"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:gravity="end">

                    <ImageView
                        android:id="@+id/img_upload"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="8dp"
                        android:src="@drawable/upload_x24"
                        android:visibility="gone" />
                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:orientation="vertical">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs_collaboration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/lightPrimary"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="4dp"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed" />
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <include layout="@layout/custom_homeowner_menus" />
    <include layout="@layout/custom_professional_menus" />
</LinearLayout>

最新更新