膨胀多个布局 [英] Inflate multiple layouts

查看:100
本文介绍了膨胀多个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个片段中,我尝试为根目录布局增加两个布局:

Inside a fragment I try to inflate two layouts besides the root layout:

View a, b;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     View root = inflater.inflate(R.layout.list_fragment, container, false);

    //..
    a = inflater.inflate(R.layout.empty_list_view, container);
    b = inflater.inflate(R.layout.progress_list_view, container);
    //..

    return root;
}

public void showB() {
    a.setVisibility(GONE);
    b.setVisibility(VISIBLE);
}

所以我只从onCreateView方法返回一个布局.但是我再给两个充气a和b.

So I just return a single layout from the onCreateView method. However I inflate two more, a and b.

但是,当我显示 b 时,实际上会显示 a .因此progress_list_view永远不会显示.有人可以解释这种奇怪的行为吗?

However when I display b actually a will be displayed. So progress_list_view never shows up. Can someone explain this strange behavior?

我怀疑a和b都添加到了容器(ViewGroup)中.而且由于a是首先添加的,因此将首先显示.

I suspect that a and b are both added to the container (ViewGroup). And since a is added first, it will be displayed first.

推荐答案

关键是,您的工作非常混乱,没有充分的理由这么做.

The point is, you're doing a huge mess and there is no good reason why you're doing it.

发生什么情况是您膨胀root,但没有通过此行View root = inflater.inflate(R.layout.list_fragment, container, false);

What happens there is that you inflate root, but not attach it to the container with this line View root = inflater.inflate(R.layout.list_fragment, container, false);

然后您将两个视图充气,并使用这些行a = inflater.inflate(R.layout.empty_list_view, container);将它们直接添加到容器中,这是按照Fragment文档的做法做的错误的事情:

Then you inflate two views and add them directly to the container with those lines a = inflater.inflate(R.layout.empty_list_view, container);, which is a wrong thing to do as per Fragment documentation:

该片段不应添加视图本身,但是可以用于生成视图的LayoutParams

The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view

ab也是同一对象,根据LayoutInflater

also both a and b are the same object, which is the container object as per documentation for the LayoutInflater

如果提供了root,则为root

If root was supplied, this is the root

和root是由您提供的,它是container,所以您拥有的基本上与a=container; b=container;

and root was supplied by you and it's the container, so what you have is basically the same asa=container; b=container;

然后返回root,这时我真的不知道由于这种混乱而发生了什么.我介绍了.

and then you return root, at which point I really don't know what is happening anymore due to this mess. I decribed.

幸运的修复很容易:

创建另一个这样的XML布局(缩短):

create another XML layout like this (shortened):

<FrameLayout>
    <include layout="@layout/empty_list_view"/>
    <include layout="@layout/progress_list_view"/>
</FrameLayout>

然后您将这个新的XML充气:

then you inflate this new XML:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View root = inflater.inflate(R.layout.new_xml, container, false);
    a = root.findViewById( .. ID of the root of empty_list_view.. )
    b = root.findViewById( .. ID of the root of progress_list_view.. )
    return root;
}

然后该代码将起作用.

原始答案:

没有奇怪的行为.您对这些布局进行了夸大,然后有2个代表它们的View对象.但是没有什么将这些Views附加到用户界面上.

There's no strange behavior. You inflated those layouts and you have 2 View objects that are representative of them. But nothing attached those Views to the UI.

仅是从onCreateView方法返回的视图,该视图将附加到设备UI.

Only the view that you return from the onCreateView method that will be attached to the device UI.

例如:

以下代码将显示a:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //..
    View a = inflater.inflate(R.layout.empty_list_view, container);
    View b = inflater.inflate(R.layout.progress_list_view, container);
    //..
    return a;
}

以下代码将显示b:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //..
    View a = inflater.inflate(R.layout.empty_list_view, container);
    View b = inflater.inflate(R.layout.progress_list_view, container);
    //..
    return b;
}

如果您想同时使用它们,则应将它们放到XML布局文件中.如果您需要可重复使用的标签,也许可以使用include标签.

if you want to have them both togeher you should put them together in the XML layout file. Maybe using an include tag if you need it re-usable.

这篇关于膨胀多个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆