开发者

Android: Fragments: setContentView alternative

开发者 https://www.devze.com 2023-03-25 23:32 出处:网络
I\'m trying to rewrite an existing project using fragments to port my application on tablet. I\'m replacing activities with fragments.

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

精彩评论

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