//这是整个布局,替换掉原来的LinearLayout,这里面的id是固定的,不能改变 <TabHost xmlns:android="http://" xmlns:tools="http://" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" ></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" /> <TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffcc00" /> <TextView android:id="@+id/tv3" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0f0" /> </FrameLayout> </LinearLayout> </TabHost> //代码中具体引用 public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost host = getTabHost(); host.addTab(host.newTabSpec("tuijian") .setIndicator("推荐") // .setContent(new Intent(MainActivity.this,OtherActivity.class))); .setContent(R.id.tv1)); host.addTab(host.newTabSpec("yule") .setIndicator("娱乐") // .setContent(new Intent(MainActivity.this,OtherActivity.class))); .setContent(R.id.tv2)); host.addTab(host.newTabSpec("junshi") .setIndicator("军事") // .setContent(new Intent(MainActivity.this,OtherActivity.class))); .setContent(R.id.tv3)); } }
TabHost
最新推荐文章于 2021-05-29 03:46:29 发布