2017-05-03 103 views
0

在我的應用程序中,我需要在兩個不同的活動中使用RecyclerView。 我想擴展布局,因爲基本佈局(回收視圖)對於兩個活動是相同的,但我不知道是否可能。 以「延長佈局」我的意思是這樣的:是否可以擴展布局?

如果這是我的base_layout.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools""> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/recyclerView"> 
    </android.support.v7.widget.RecyclerView> 
</RelativeLayout> 

我願做這樣的事情在我child_layout.xml

include "base_layout.xml" 
<Button 
      android:id="@+id/btnAddVehicle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="+" 
      android:onClick="launchAddVehicleActivity"/> 

因此,使用chid_layout.xml的活動將獲得RecyclerViewButton裏面。

是否有可能做這樣的事情?如果你想添加你需要編寫一個自定義組件裏面的佈置按鈕

回答

1

您可以創建一個通用的佈局,然後將其添加在你的佈局的活動。

通用佈局 my_generic_layout.xml

<RelativeLayout 
android:id="@+id/generic_layout"> 
    <RecyclerView/> 
</RelativeLayout> 

佈局的活動

<RelativeLayout 
android:id="@+id/layout_1"> 
<include layout="@layout/my_generic_layout" /> 
</RelativeLayout> 


<RelativeLayout 
android:id="@+id/layout_2"> 
<include layout="@layout/my_generic_layout" /> 
</RelativeLayout> 
+0

它是正是我想要的,非常感謝你:) – Clyky

0

只是用這個<include layout="@layouts/chid_layout" />