【问题标题】:Creating a custom widget with other widgets included创建包含其他小部件的自定义小部件
【发布时间】:2015-07-10 00:45:21
【问题描述】:

我对 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 和工具栏的一堆样板代码。

我觉得我只是错过了一些明显的东西。

【问题讨论】:

    标签: android widget code-reuse reusability


    【解决方案1】:

    您需要创建继承自 DrawerLayout 的新类,扩充您的视图,设置参数。

    教程是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2023-03-20
      • 2019-11-20
      • 1970-01-01
      相关资源
      最近更新 更多