首页 文章

在运行时在已膨胀的层次结构中添加视图/布局?

提问于
浏览
1

我想知道在运行时将视图层次结构添加到已经膨胀的视图中是不好的做法 .
让我们考虑一下我的例子 . 我有以下Activity类层次结构

  • 基本活动 - 一些基本活动,包括生命周期方法的基本配置,不扩展布局(setContentView) .

  • SingleFramgentActivity它在 onCreate 方法中有setContenview方法 . 此活动的布局如下所示

C

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/fragment_container"
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

    </FrameLayout
    <ViewStub
        android:id="@+id/stub_progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/progress_bar_buttons"
        android:layout="@layout/inflate_stub_progressbar_bg"/>
</FrameLayout>

这里有几个问题 .
正如你在这里看到的那样,有一个片段容器 FrameLayout 用于保存当前片段,因为它是 SingleFragmentActivity .

我需要做的是在我的布局中添加工具栏,并且应该出现在此活动内的所有片段中 .
在这种情况下我的布局必须看起来像

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/fragment_container"
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

    </FrameLayout>
    <include
        android:id="@+id/toolbar"
        layout="@layout/include_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"/>
    <ViewStub
        android:id="@+id/stub_progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/progress_bar_buttons"
        android:layout="@layout/inflate_stub_progressbar_bg"/>
</FrameLayout>

这里有几个问题 .
我已经在 SingleFramgentActivity 课程中夸大了布局 . 所以我只能添加到已经膨胀的层次结构,获取根视图并调用 addView 方法 . 如果是这样,请看下一个问题 .
2.我只需要添加一些视图,我需要添加布局文件来查看BETWEEN两个已经存在的视图的层次结构 . FrameLayout和ViewStub是因为Z在FrameLayout中放置了视图 .

这个问题有几个解决方案
1.在我需要的每个片段中包含工具栏(在活动中的所有片段中)由于代码重复,这似乎不是最好的方法 . (不要重复自己的规则) .
2.使用一些基本的ToolbarFragment类,在这种情况下,我必须在运行时通过继承的片段添加所有必需的视图 .
3.使用一些Decorator类ToolbarActivity,如上所述,添加到已经膨胀的视图层次结构中 . 4.使用与SingleFramgentActivity相同的布局,但是将工具栏包含标记并将类重命名为SingleToolbarFragmentActivity . 似乎单一责任原则刚刚被打破 . 如果只需要工具栏没有片段容器,反之亦然 .

所以我需要建议最好的方法是什么 . 关于性能的做法是不是很糟糕?
请建议在这种情况下最好使用哪种方法不会导致性能问题并使代码尽可能可读

1 回答

  • 1

    如果您没有逐个添加视图,或者更改其布局边界,或者同样多次调用 requestLayout/invalidate ,则在运行时添加视图没有性能问题 . 除此之外不应该导致任何性能问题 .

    鉴于此,我通常在你的情况下做的是创建一个让我们称之为 BaseActivity 的方法可以像 hasToolbar():boolean 那样扩展,并且在创建这个活动的那一刻,我膨胀所需的东西并将其添加到视图中 . 这是一种方法,但是你提到的任何一种方法都可以在没有性能问题的情况下工作,因为膨胀单个布局并不是那么耗时(例如在ListViews中发生) .

相关问题