开发者

Android: Fragments: setContentView alternative

I'm trying to rewrite an existing project using fragments to port my application on tablet.

I'm replacing activities with fragments.

The problem is that I don't know what is the equivalent to setContentView method? Is there any way to create Fragment's view except 开发者_StackOverflow中文版rewriting onCreateView?


In the onCreateView() method you can return the view of your fragment as follows :

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


I'm not sure why you cannot use onCreateView. But here is what you can do.

Create LinearLayout object, save it as mRooLayout member field and return it from onCreateView. Here is how you can implement setContentView:

void setFragmentContentView(View view)
{
    LinearLayout.LayoutParams layoutParams = 
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                                          ViewGroup.LayoutParams.FILL_PARENT);
    mRootLayout.removeAllViews();
    mRootLayout.addView(view, layoutParams);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜