我有一个沙箱项目,一切正常,但不是真正的项目。 我想我错过了什么......
在我的主要活动中(我尽可能地简化了一个项目):
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(createUI());
}
public View createUI() {
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.HORIZONTAL);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
LinearLayout leftLayout = new LinearLayout(this);
leftLayout.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
leftLayout.setId(11111);
android.widget.TextView textView = new android.widget.TextView(this);
textView.setText("112233");
rootLayout.addView(textView);
rootLayout.addView(leftLayout);
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ModelEditorFragment simpleFragment = new SimpleFragment();
transaction.add(11111, simpleFragment);
}
return rootLayout;
}
在SimpleFragment.java中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
TextView textView = new TextView(getActivity());
textView.setText("SimpleFragmentText");
return textView;
但是当我开始时,我只看到没有SimpleFragmentText的112233。 调试时我注意到onCreateView方法永远不会被调用...为什么?看起来相同的代码在独立应用程序中运行良好...可能还有其他我不知道的事情吗?
答案 0 :(得分:2)
我忘了transaction.commit:
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ModelEditorFragment simpleFragment = new SimpleFragment();
transaction.add(11111, simpleFragment);
transaction.commit();
}
答案 1 :(得分:-2)
rootLayout.addView(textView);
{
rootLayout.addView(leftLayout);
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ModelEditorFragment simpleFragment = new SimpleFragment();
transaction.add(11111, simpleFragment);
}
}
return rootLayout;
尝试我认为你的rootLayout只是缺少textView添加部分