我的Android心得(7)--TabActivity

要得到这样的界面:一个大的框架下有三个tab,tab之间可以切换


一、TabWidget

这是我的实现

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <LinearLayout  
  android:orientation="vertical"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    >
     
     <TabWidget
      android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      />
     <FrameLayout
      android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      />
      

    </LinearLayout>
</TabHost>

注意tabhost 和 tabs 都是系统的id,不可以重新命名
android:id="@android:id/tabhost"<pre name="code" class="html">android:id="@android:id/tabs"


MainActivity.java :

public class MainActivity extends TabActivity{
	
	private TabHost tabhost;
	public static Target target;
	
	public void onCreate(Bundle saveInstanceState){
		super.onCreate(saveInstanceState);
		setContentView(R.layout.activity_main);
		
		tabhost = getTabHost();
		tabhost.addTab(tabhost.newTabSpec("tab_1").setIndicator("提醒", getResources().
				getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, RemindActivity.class)));
		
		tabhost.addTab(tabhost.newTabSpec("tab_2").setIndicator("设置", getResources().
				getDrawable(R.drawable.setting_tab_icon)).setContent(new Intent(this, SettingActivity.class)));
		
		tabhost.addTab(tabhost.newTabSpec("tab_3").setIndicator("关于", getResources().
				getDrawable(R.drawable.about_tab_icon)).setContent(new Intent(this, AboutActivity.class)));
		
		// 设置默认在第一个tab
		tabhost.setCurrentTab(0);
		/*tabhost.setOnTabChangedListener(new OnTabChangeListener(){

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals("tab_1")){
					Toast.makeText(MainActivity.this, "in tab 1", Toast.LENGTH_SHORT).show();
				}
				else if(tabId.equals("tab_2")){
					Toast.makeText(MainActivity.this, "in tab 2", Toast.LENGTH_SHORT).show();
				}
				else if(tabId.equals("tab_3")){
					Toast.makeText(MainActivity.this, "in tab 3", Toast.LENGTH_SHORT).show();
				}
			}
			
		});*/
	}
}

要实现选中某个tab,tab的图片会变化,要加入个selector.xml

http://www.apkbus.com/forum.php?mod=viewthread&tid=140042


二、ActivityGroup+Button

网上也有人是这样实现的,类似上面


三、ViewPager

高大上,现在的微信就是ViewPager
http://blog.csdn.net/wangjinyu501/article/details/8144332

你可能感兴趣的:(我的Android心得(7)--TabActivity)