创建与其他小部件自定义部件包括

问题描述:

我是相当新的android开发,我有一个很难搞清楚创建可重用的代码最简单的方法。我在google上发现了不少文章,android有许多零件和解决方案,我无法看到优点和缺点,所以我只会发布我想要做的事情,然后看看弹出的内容。创建与其他小部件自定义部件包括

目前我有:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <RelativeLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/widget_toolbar"/> 

     <ListView 
      android:background="@color/amber_A700" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/toolbar"> 

     </ListView> 

    </RelativeLayout> 

    <apps.new.app.ui.widget.ScrimInsetsFrameLayout 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/linearLayout" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#ffffffff" 
     android:fitsSystemWindows="true" 
     app:insetForeground="#4000" > 

     <fragment android:id="@+id/navigation_drawer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:name="com.example.newapp.NavigationDrawerFragment" 
      tools:layout="@layout/frag_nav_drawer" /> 

    </apps.new.app.ui.widget.ScrimInsetsFrameLayout> 

</android.support.v4.widget.DrawerLayout> 

基本上我想要的是让这个在我可以在任何项目中,我想用一个更简洁的风格使用库:

<android.my.custom.widget 
    ... 
    app:drawerFragment="drawer_fragment_name" 
    ...> 

    //Listview would go here 

</android.my.custom.widget> 

这允许我设置抽屉碎片,自动包含“ScrimInsetsFrameLayout”,自动包含工具栏。基本上我想要一个可重用的库,我可以更新它,并将它推送到我使用它的任何应用程序中。使用android dev的最佳做法是什么?如果这只是一个自定义小部件,我将如何去包含其他小部件并在其中插入片段?

最终这将让我简化代码一堆只看到究竟是什么活动,而不是看到一堆的锅炉板代码为抽屉式导航的RelativeLayout和工具栏。

我觉得我只是缺少一些明显的东西。

您需要创建新的类,它从DrawerLayout继承,虚增您的意见,设置PARAMS。

教程是here