片段 - 你必须使用一个活动包装的片段包括整个活动在吗? [英] Fragments - Do you have to use an Activity Wrapper around a fragment which comprises the whole Activity?

查看:148
本文介绍了片段 - 你必须使用一个活动包装的片段包括整个活动在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑从 developers.android.com

本介绍如何使用片段,像这样:

This describes using Fragments like so:

  • 在一个电话,您可以使用片段1活性A和片段2活动B.
  • 在平板电脑,你有更多的房地产,所以你用片段1和片段2的活性方法。

太棒了! ...但是...在第一个例子中(一个与电话),您创建活动包含一个℃的xml文件;片断> ,这一切,在活动中,你只叫的setContentView()在该XML?这似乎是一个很大的冗余code(活动,XML和放大器;片段显示片段):你可以设置一个片段活动或者是XML格式的包装总是需要?

Great! ... But... On the first example (the one with a phone) you create an Activity with an xml file containing a single <fragment> and that's all, in the activity you only call setContentView() on that xml? That seems like a lot of redundant code (Activity, XML & Fragment to display a Fragment): Can you set a Fragment as an Activity or is a Wrapper with XML always required?

推荐答案

嗯,发现它的这里

public class MainMenuHolder extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        // If not already added to the Fragment manager add it. If you don't do this a new Fragment will be added every time this method is called (Such as on orientation change)
        if(savedInstanceState == null)
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, new MainMenuFragment()).commit();
    }
}

FragmentActivity允许的用户设置片段 android.R.id.content 的内容,我认为是主干鉴于Android的内部ID。

FragmentActivity allow's you to set the Fragment as the content of android.R.id.content which I assume is the android internal ID of the trunk view.

通过这个方法,你最终仍然有大部分冗余性(如果你想要的是片段作为活动)。但尽管如此,一半多绒毛作为具有活性并用作容器的XML文件。

With this method you still end up with an mostly redundant activity (If all you want is the Fragment acting as the Activity). But still, half as much fluff as having an activity and an XML file acting as a container.

任何其他的答案将是AP preciated!

Any other answers would be appreciated!

这篇关于片段 - 你必须使用一个活动包装的片段包括整个活动在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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