簡體   English   中英

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

[英]How do I reuse the same xml background layout in multiple activities/fragments in Android Studio?

我正在開發一個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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM