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

时间:2017-06-21 06:27:03

标签: android android-layout android-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>

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

1 个答案:

答案 0 :(得分:0)

需要在其下方调用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>