如何在没有片段的活动之间共享公共布局

时间:2014-11-19 08:01:05

标签: android layout android-activity

有没有可能的方法在活动之间共享布局(部分)?例如,在我的应用程序中,所有活动都有类似的布局,顶部是长操作指示器(进度条,在没有执行任何操作时隐藏),底部用于显示错误。所有活动只有中间部分不同。见下图。

enter image description here

所以我的问题是,是否可以为我的应用中的所有活动重用公共布局(加载和错误部分)? (目前我不想因为某些原因使用片段来完成它)

也许布局资源应该是这样的:

layoutfolder

activity_common.xml

activity_one_content.xml

activity_two_content.xml

感谢

3 个答案:

答案 0 :(得分:16)

您可以创建一个抽象的基础'所有活动扩展的活动,覆盖setContentView以合并基础和子活动布局。

这样您就可以处理基本活动中的所有加载/错误代码,只需在隐藏和显示子活动中的视图之间切换。

抽象活动:

public abstract class BaseActivity extends Activity {

    protected RelativeLayout fullLayout;
    protected FrameLayout subActivityContent;

    @Override
    public void setContentView(int layoutResID) {
        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null);  // The base layout
        subActivityContent = (FrameLayout) fullLayout.findViewById(R.id.content_frame);            // The frame layout where the activity content is placed.
        getLayoutInflater().inflate(layoutResID, subActivityContent, true);            // Places the activity layout inside the activity content frame.
        super.setContentView(fullLayout);                                                       // Sets the content view as the merged layouts.
    }

}

布局文件:

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


    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/loading_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/error_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

答案 1 :(得分:4)

您可以在include中使用XML,包括布局代码中可重复使用的部分。

举个例子,这是我在我的应用中使用的Toolbar的布局文件:

// /res/layout/component_toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:taggr="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    android:minHeight="?attr/actionBarSize"
    taggr:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    taggr:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

现在,如果我想在另一个Toolbar中再次使用Activity,那么这就是我要写的全部内容:

// /res/layout/whatever_layout_this_might_be.xml

<include layout="@layout/component_toolbar" />

请记住,这将仅复制布局 - 而不是所述小部件/组件的实际行为。

如果您想要实际复制所有方面(布局,行为),我担心Fragment是唯一的方法。

答案 2 :(得分:0)

尽管API 13已弃用ActivityGroup,但如果您不想使用片段,那么这可能是您的最佳选择。

根据文档,ActivityGroup是:

  

包含并运行多个嵌入式活动的屏幕。

您可以找到教程herehere虽然上面提到的教程使用Tablayout,但您可以将其替换为XML中的常用布局。

第二种方法可能是Reuse the layout with include tag,在这种方法中,你可以在应用程序的任何地方重复使用曾经创建的公共布局。

相关问题