Android的 - 在运行时更改片段布局 [英] Android - Change fragment layout in runtime

查看:214
本文介绍了Android的 - 在运行时更改片段布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个单一的活动多片段设计我的应用程序。我计划有几个屏幕(片段的布局),我会的(可能添加到后台堆栈)在code开关。

I would like to implement a Single-Activity Multi-Fragments design in my app. I plan to have several "screens" (layouts of Fragment) that I'll switch between (possibly adding to back-stack) in code.

据我了解片段中每个屏幕的布局使用布局对象(例如的FrameLayout),它作为占位符碎片(共享相同的ID)设置。由于不同的屏幕有不同的片段安排(一个可能的FrameLayout,另外的LinearLayout等),我想知道:在运行时的片段布局之间如何切换

To my understanding the layout of the fragments in each screen is set using Layout objects (e.g. FrameLayout), which act as placeholders for the fragments (sharing the same ID). Since different screens have different Fragment arrangements (one could be FrameLayout, and another LinearLayout, etc.) I was wondering: How do I switch between layouts of fragments in runtime?

我明白添加/(通过FragmentManager)更换碎片,但我想完全添加一个新的布局,包括他们的现场活动中。有点像有交易记录的setContentView......

I understand adding / replacing Fragments (via FragmentManager), but I'd like to completely add a new layout that contains them, within a live activity. Kind of like having transactions for "setContentView"...

我如何做到这一点? 谢谢!丹尼。

How do I do this? Thanks! Danny.

推荐答案

这当然是可能的,你需要做的唯一事情是生成自己的ID。该ID可以是任何东西,但他们不能与AAPT ID冲突(那些在R),且不得为负。

It's certainly possible, the only thing you need to do is to generate your own IDs. The IDs may be anything but they must not conflict with the aapt IDs (the ones in R) and must not be negative.

下面的例子演示了这种用一组固定的ID:

The following example demonstrates this with a set of fixed IDs:

public class MainActivity extends Activity {
    private final int ID_TABLE = 0xA;
    private final int ID_ROW1 = 0xB;
    private final int ID_ROW2 = 0xC;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout ll = (LinearLayout) findViewById(R.id.root);
        TableLayout tl = new TableLayout(this);
        tl.setId(ID_TABLE);
        TableRow tr1 = new TableRow(this);
        tr1.setId(ID_ROW1);
        TableRow tr2 = new TableRow(this);
        tr2.setId(ID_ROW2);
        tl.addView(tr1);
        tl.addView(tr2);
        ll.addView(tl);

        MyFragment frag1 = new MyFragment();
        MyFragment frag2 = new MyFragment();
        MyFragment frag3 = new MyFragment();
        MyFragment frag4 = new MyFragment();

        getFragmentManager().beginTransaction()
            .add(ID_ROW1, frag1, "cell1_1")
            .add(ID_ROW1, frag2, "cell1_2")
            .add(ID_ROW2, frag3, "cell2_1")
            .add(ID_ROW2, frag4, "cell2_2")
            .commit();
        getFragmentManager().executePendingTransactions();
    }
}

为了切换到不同的布局,您可以删除片段和其他地方进行添加。
让我知道如何去。

In order to switch to a different layout, you can remove the fragments and add them elsewhere.
Let me know how it goes.

编辑:澄清,视图和ViewGroups不需要被实例化一次,然后保持为活动的寿命。只要确保任何片段删除或者消除他们的相关视图之前分离。此外,如果您创建和删除的onCreate以外的意见,你应该确保它可以使用的onSaveInstanceState并重复该过程中的onCreate恢复。读到这里的图和有关的配置更改

to clarify, Views and ViewGroups don't need to be instantiated once and then kept for the lifetime of the Activity. Just make sure any fragments are either removed or detached before removing their associated view. Also, if you create and remove views outside of onCreate you should make sure it can be restored by using onSaveInstanceState and repeating the process in onCreate. Read the diagram here and the paragraph about configuration changes.

这篇关于Android的 - 在运行时更改片段布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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