如何在选项卡式活动中使用主详细信息流?
我有一个有3页的视图寻呼机。我正在尝试使用android studio提供的主/详细信息流作为视图寻呼机中的一个片段。
答案 0 :(得分:2)
您可以尝试这样做:
在onCreateView上添加方法并从onCreate移动所有内容,super.onCreate()
和setContentView():
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentActivity faActivity = (FragmentActivity) super.getActivity();
// Replace LinearLayout by the type of the root element of the layout you're trying to load
LinearLayout llLayout = (LinearLayout) inflater.inflate(R.layout.activity_layout, container, false);
// Of course you will want to faActivity and llLayout in the class and not this method to access them in the rest of
// the class, just initialize them here
//此前onCreate()的内容 // ...
// Don't use this method, it's handled by inflater.inflate() above :
// setContentView(R.layout.activity_layout);
// The FragmentActivity doesn't contain the layout directly so we must use our instance of LinearLayout :
llLayout.findViewById(R.id.someGuiElement);
// Instead of :
// findViewById(R.id.someGuiElement);
return llLayout; // We must return the loaded Layout
}
删除onCreate方法。
super.getActivity()
替换的内容。或者使用onCreateView中保存的值,如2)所示。示例:Intent i = getIntent();
变为Intent i = super.getActivity().getIntent()