2017-06-21 75 views
0

我正在開發一個幾乎每個活動都有相同背景圖像和底部導航欄的android應用程序。如何在Android Studio中的多個活動/片段中重複使用相同的xml背景佈局?

我想,而不是重複使用的每個時間寫出來的驗證碼

這裏是我的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

使上述代碼的XML和使用 <包括 佈局=「@佈局/ your_xml_name」 /> 並使用該行上您的應用程序的XML –

+0

會把所有的每一個佈局我這一行後放在主要的xml佈局中,在模板xml文件中的LinearLayout中考慮? –

+0

@ScottWeller你可以把include佈局放在線性佈局,相對等任何父佈局中。是的,你可以在

回答

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> 
+0

我明白這一部分。我的問題是,我如何將其納入一項新活動並在其基礎上進行構建?請問android studio會在xml文件中的線性佈局中包含include聲明後放置的任何代碼嗎? –

+0

@ ScottWeller-添加包含意味着無論您在「包含的佈局」中編寫的任何代碼的行爲如果您將整個代碼寫入她的代碼,而不是包含代碼,它們的行爲也會相同。 根據我的說法,你首先試着自動了解它是如何工作的。 –

相關問題