简体   繁体   English

如何在Android中将活动的根视图设置为片段?

[英]How to set an Activity's root view as fragment in Android?

I have a Fragment, and I want to set that whole fragment as root view of my activity. 我有一个片段,我想将整个片段设置为活动的根视图。 I have everything ready, and I'm instantiating my fragment programatically. 我已经准备好一切,并且正在以编程方式实例化片段。 I've tried (in my activity): 我已经尝试过(在我的活动中):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FeedFragment fragment = [...];
    setContentView(fragment.getView());
}

But I've got a null pointer exception. 但是我有一个空指针异常。 In other words, how can I make my fragment act like an activity? 换句话说,如何使我的片段表现得像活动? I only target ICS+, I don't need to support older versions, if it makes any difference. 我只针对ICS +,如果有任何不同,我不需要支持旧版本。

Try this 尝试这个

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.all_lecturer_frag, container, false);

     ......

    return rootView;
   }
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
  setContentView(R.layout.xxx);
  //initializations...    
    if (savedInstanceState == null) {
        // During initial setup, plug in the fragment.
        YourFragment details = new YourFragment();
        getFragmentManager().beginTransaction().add(R.id.your_root_frame_layout, details).commit();
    }
}

A Fragment, by design, is intended to be a tool to help you reuse screen space and as such, fragments have to be present inside a container. 通过设计,片段旨在用作帮助您重用屏幕空间的工具,因此,片段必须存在于容器内。 So while a fragment cannot technically be a root view, you can have a fragment be the only view inside the Activity. 因此,虽然片段从技术上讲不能成为根视图,但您可以让片段成为Activity中的唯一视图。 For this, you should inflate the view for your fragment programmatically inside the onCreateView() method of the fragment. 为此,您应该以编程方式在片段的onCreateView()方法内为片段的视图充气。 then you could have something like this in your activity's layout xml: 那么您可以在活动的布局xml中添加以下内容:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.package.fragment_name
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</FrameLayout>

And then, within your activity, all you have to do is: 然后,在您的活动中,您要做的就是:

setContentView(R.layout.main);

Since, the fragment is defined in the layout xml, it cannot be removed from the activity's layout (although the layout itself can be changed) and is tied to it. 由于该片段是在布局xml中定义的,因此无法将其从活动的布局中删除(尽管可以更改布局本身)并将其绑定。

Also, on a side note, notice that the root view is a FrameLayout and not the fragment itself. 另外,请注意,根视图是FrameLayout,而不是片段本身。 But in this manner, your fragment can be tied to the activity. 但是通过这种方式,您的片段可以绑定到活动。 But don't forget that the Fragment will still retain it's lifecycle separate from the activity's. 但是不要忘记,Fragment仍将保留其活动的生命周期。

EDIT: If you need to create your fragment instance programmatically, you have to do: 编辑:如果您需要以编程方式创建片段实例,则必须执行以下操作:

getFragmentManager().beginTransaction().add(R.id.frame_layout, your_fragment).commit();

This is the only way to add your fragment programmatically. 这是以编程方式添加片段的唯一方法。 But also keep in mind that the Fragment's layout is not tied to the activity's layout. 但也要记住,Fragment的布局不与活动的布局绑定。 But you can use the Fragment's lifecycle to behave similarly as an Activity. 但是您可以使用Fragment的生命周期,使其行为类似于Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM