对于单个布局 XML 文件来说,多少个 ViewStub 太多了?

发布于 2024-09-26 09:22:06 字数 948 浏览 5 评论 0原文

我在 XML 文件 (base_layout.xml) 中定义了一个布局,除了 3-5 个其他视图(例如 ImageView<)之外,该布局还可能包含 20 多个 ViewStub 定义。 /code> 和一个包含 3-5 个 ImageButton 视图的 LinearLayout

我应该关心我在此布局文件中放置了多少 ViewStub 视图吗?

我在developer.android网站上读到:

ViewStub 是一个愚蠢且轻量级的 看法。它没有维度,它没有 画任何东西并且不参与 以任何方式在布局中。这意味着 ViewStub 非常便宜 充气并且非常便宜地保存在 查看层次结构

是否足够便宜,可以拥有 20 多个视图层次结构?当然,并不是所有的都被夸大,一次只是 1-2 个。

当我说足够便宜或谈论关心时,我指的是UI的性能

编辑: 我正在努力实现的目标: 创建一个布局 XML 文件,该文件可以作为我所有活动的骨架。在每个Activity中,我将使用活动的布局来填充正确的ViewStub。由于我有很多活动需要相同的骨架,因此我希望尽可能地重用

我有一个 Activity 类,它是几乎所有活动的父级。该父类调用setContentView(R.layout.base_layout);。对于每个子活动,我所做的就是在 base_layout.xml 中膨胀相应的 ViewStub。这样做可以让我拥有一个非常定制的 UI,并且在所有活动布局上使用相同的骨架视图

I have a layout defined in an XML file(base_layout.xml) which may contain 20+ ViewStub definitions in addition to 3-5 other views such an ImageView and a LinearLayout containing 3-5 ImageButton views.

Should i be concerned about how many ViewStub views i place in this layout file?

I read on the developer.android site:

A ViewStub is a dumb and lightweight
view. It has no dimension, it does not
draw anything and does not participate
in the layout in any way. This means
that a ViewStub is very cheap to
inflate and very cheap to keep in a
view hierarchy

is it cheap enough to have 20+ of them? not all being inflated of course, just 1-2 at a time.

when i say cheap enough or talk of being concerned, i am regarding performance of the UI

edit:
what i am trying to accomplish:
create a layout XML file which can be the skeleton for all my of activities. in each Activity, i will inflate the correct ViewStub with the activity's layout. Since i have so many activities requiring the same skeleton, i wish to re-use as much as possible

I have an Activity class which is the parent of almost all of my activities. this parent class calls setContentView(R.layout.base_layout);. for each child activity, all i am doing is inflating the corresponding ViewStub inside base_layout.xml. doing this allows me to have a very customized UI with the same skeleton view used on all of my activity layouts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-10-03 09:22:06

我认为您不会看到性能受到很大影响。它仍然比从一开始就将它们全部充气要便宜。

拥有如此多存根的缺点是您可能会忽视整个设计。也许将多个视图/项目分组到一个视图组中更有意义。也许你可以解释一下你正在尝试做什么,看看是否有更好的方法来实现它

编辑:
好吧,不要拥有多个包含不同子视图的 ViewStub,比如

<ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/activity1"
           android:layout="@layout/myActivity1"
           />
<ViewStub android:id="@+id/stub2"
           android:inflatedId="@+id/activity2"
           android:layout="@layout/myActivity2"
           />

只有一个 ViewStub 并在您的活动 onCreate() 中执行类似

setContentView(R.layout.base_layout);
ViewStub stub = (ViewStub)findViewById(R.id.stub);

stub.setInflateId(R.id.activity1);
stub.setLayoutResource(R.layout.myActivity2);
stub.inflate();

这样的操作,这样您的 base_layout 中仍然只有一个 ViewStub,您可以在膨胀之前在代码中设置它。

I don't think you'll see a big performance hit. It's still cheaper than having all of them inflated from the begining.

The downside of having so many stubs is that you could lose sight of the whole design. Maybe it makes more sense to group several views/items into one viewgroup. Maybe you could explain what you're attempting to do and see if there is a better way to realize it

edit:
Well, instead of having multiple ViewStubs which include different subviews like

<ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/activity1"
           android:layout="@layout/myActivity1"
           />
<ViewStub android:id="@+id/stub2"
           android:inflatedId="@+id/activity2"
           android:layout="@layout/myActivity2"
           />

just have a single ViewStub and in your acitivities onCreate() do something like

setContentView(R.layout.base_layout);
ViewStub stub = (ViewStub)findViewById(R.id.stub);

stub.setInflateId(R.id.activity1);
stub.setLayoutResource(R.layout.myActivity2);
stub.inflate();

This way you'd still have only one ViewStub in your base_layout, which you could setup in code before inflating.

酒废 2024-10-03 09:22:06

LinearLayoutRelativeLayoutViewGroup 的扩展

,所以我使用 ViewGroup 作为参数
这是我的解决方案

public CategoryViewController(Context context, ViewGroup containerView) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        View categoryLayout = inflater.inflate(R.layout.category_section, null, false);
        containerView.addView(categoryLayout);
        this.categoryView = categoryLayout;
}

LinearLayout or RelativeLayout is extend of ViewGroup

so i used ViewGroup as parameter
this is my solution

public CategoryViewController(Context context, ViewGroup containerView) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        View categoryLayout = inflater.inflate(R.layout.category_section, null, false);
        containerView.addView(categoryLayout);
        this.categoryView = categoryLayout;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文