此行显示您创建的TabHost
fragment_main.xml位于布局目录内部,该目录由PlaceholderFragment使用
tools:context="com.example.app.MainActivity$PlaceholderFragment"
但是您在其中找到了TabHost
activity_main.xml
将代码从PlaceholderFragment的
onCreate()移到
onCreateView,如果您使用的是AS附带的默认模板,则在同一类的下面
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); TabHost tabHost = (TabHost) rootView.findViewById(R.id.tabHost); tabHost.setup(); TabHost.TabSpec spec1, spec2, spec3; spec1 = tabHost.newTabSpec("spec1"); spec1.setContent(R.id.tab1); spec1.setIndicator("Tab1"); tabHost.addTab(spec1); spec2 = tabHost.newTabSpec("spec2"); spec2.setContent(R.id.tab2); spec2.setIndicator("Tab2"); tabHost.addTab(spec2); spec3 = tabHost.newTabSpec("spec3"); spec3.setContent(R.id.tab3); spec3.setIndicator("Tab3"); tabHost.addTab(spec3); return rootView; }或者,如果您愿意,也可以在
activity_main.xml不更改Java代码的情况下将TabHost移至其中,但是不建议使用具有很多优点的片段。



