2017-05-03 13 views
0

私のアプリケーションでは、RecyclerViewを2種類のアクティビティで使用する必要があります。 基本レイアウト(リサイクラビュー)が2つのアクティビティで同じであるため、レイアウトを拡張しようとしていましたが、可能かどうかわかりません。これは私の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

あなたは一般的なレイアウトを作成することができますを膨張RelativeLayoutから継承するクラスを作成することができますAndroidのドキュメントを参照してください。

ジェネリックレイアウト 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" />