我的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值