[英]Android: Fragments: setContentView alternative
我正在嘗試使用fragments
重寫現有項目以在平板電腦上移植我的應用程序。
我正在用fragments
替換活動。
問題是我不知道setContentView
方法的等價是什么? 除了重寫onCreateView
之外,有沒有辦法創建Fragment's
視圖?
在onCreateView()
方法中,您可以返回片段的視圖,如下所示:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.ads_tab, container, false);
}
我不確定為什么你不能使用onCreateView
。 但這是你能做的。
創建LinearLayout
對象,將其另存為mRooLayout
成員字段並從onCreateView
返回。 以下是如何實現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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.