【发布时间】:2011-08-04 08:50:50
【问题描述】:
我正在尝试使用fragments 重写现有项目以将我的应用程序移植到平板电脑上。
我正在用fragments 替换活动。
问题是我不知道setContentView方法等价于什么?除了重写onCreateView,还有什么方法可以创建Fragment's 视图?
【问题讨论】:
我正在尝试使用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);
}
【讨论】:
onCreateView。好吧,您可以,但这不会产生任何影响,并且很可能会弄乱您的片段中的所有内容(如果您在此函数中保存对视图的引用)。此函数只能由操作系统调用。