首页 > 代码库 > Tabhost最纯净的实现方式
Tabhost最纯净的实现方式
有时候常常使用别人用Tabhost+其他的实现demo。单纯利用Tabhost该怎样使用呢?
以下看样例:
public class MainActivity extends TabActivity { public TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取对象 tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("index").setIndicator("实时新闻") .setContent(new Intent(this, IndexActivity.class))); tabHost.addTab(tabHost.newTabSpec("center").setIndicator("我的空间") .setContent(new Intent(this, MiddleActivity.class))); tabHost.addTab(tabHost.newTabSpec("self").setIndicator("设置") .setContent(new Intent(this, LastActivity.class))); // 指定的当前的tab // 通过索引指定 索引从0開始 // 即一開始要显示的是哪一页 tabHost.setCurrentTab(0); // 从零開始 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }二、效果图
(2)Tabhost选项卡置于底部的样例
採用LinearLayout布局。设置Weight属性就能够将选项卡位于底部
1、xml布局文件
<?2、MainActivityxml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> </LinearLayout> </TabHost>
public class MainActivity extends ActivityGroup { private TabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTabs(); } private void initTabs() { mTabHost = (TabHost) findViewById(R.id.tabhost); mTabHost.setup(this.getLocalActivityManager()); Intent intent = new Intent(this, HomepageActivity.class); mTabHost.addTab(mTabHost.newTabSpec("Tab1") .setIndicator(getString(R.string.homepage_indicator), getResources().getDrawable(R.drawable.homepage_indicator_selector)) .setContent(intent)); intent = new Intent(this, BillboardActivity.class); mTabHost.addTab(mTabHost.newTabSpec("Tab2") .setIndicator(getString(R.string.billboard_indicator), getResources().getDrawable(R.drawable.billboard_indicator_selector)) .setContent(intent)); intent = new Intent(this, MyLotteryActivity.class); mTabHost.addTab(mTabHost.newTabSpec("Tab3") .setIndicator(getString(R.string.mylottery_indicator), getResources().getDrawable(R.drawable.mylottery_indicator_selector)) .setContent(intent)); intent = new Intent(this, MoreActivity.class); mTabHost.addTab(mTabHost.newTabSpec("Tab4") .setIndicator(getString(R.string.more_indicator), getResources().getDrawable(R.drawable.more_indicator_selector)) .setContent(intent)); } }3、效果图
Tabhost最纯净的实现方式