在片段中设置新的布局 [英] Set new layout in fragment

查看:69
本文介绍了在片段中设置新的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在特定条件下在运行时更改片段的布局.

I'm trying to change the layout of a fragment during runtime under a particular condition.

onCreateView()中膨胀的初始布局:

The initial layout in inflated within the onCreateView():

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.cancel_video, null);
    }

然后稍后在片段代码中,我想用其他布局替换初始布局.

Then sometime later within the fragment code I would like to replace the initial layout with some other layout.

到目前为止,我已经尝试了一些方法;这是我的最新消息:

I've tried a few things so far; this is the latest that I have:

private void Something(){
    if(checkLicenseStatus(licenseStatus, statusMessage)){
                View vv = View.inflate(getActivity(), R.layout.play_video, null);
                //more code
    }
}

我该怎么做?

推荐答案

一旦片段膨胀,就无法替换它的布局.如果需要条件布局,则必须重新设计布局并将其分解为甚至更小的元素,例如Fragments.或者,您可以将所有布局元素分组到子容器中(例如LinearLayout),然后将它们全部包装在RelativeLayout中,放置它们以使其彼此重叠,然后在出现以下情况时用setVisibility()切换这些LinearLayout的可见性并根据需要.

You cannot replace the fragment's layout once it is inflated. If you need conditional layouts, then you either have to redesign your layout and break it down into even smaller elemens like Fragments. Alternatively you can group all the layout elements into sub containers (like LinearLayout), then wrap them all in RelativeLayout, position them so they overlay each other and then toggle the visibility of these LinearLayouts with setVisibility() when and as needed.

这篇关于在片段中设置新的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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