ActionBar导航

宋鸿
2023-12-01
private TabListener listener = new TabListener() {

		@Override
		public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
			int position = tab.getPosition();
			// 放的是角标,所以从0开始
			switch (position) {
			case 0:
				replace(fragment_tuijian);
				break;
			case 1:
				replace(fragment_yeshi);
				break;
			case 2:
				replace(fragment_hougong);
				break;
			case 3:
				replace(fragment_jiemi);
				break;

			default:
				break;
			}

		}

		@Override
		public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}
	};



protected void createActionBar() {
		// 得到ActionBar对象
		ActionBar actionBar = getActionBar();
		// 设置标题
		actionBar.setTitle("1409A");
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		// 设置导航类型
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// 添加标签页
		actionBar.addTab(actionBar.newTab().setText("推荐")
				.setTabListener(listener));
		actionBar.addTab(actionBar.newTab().setText("野史")
				.setTabListener(listener));
		actionBar.addTab(actionBar.newTab().setText("后宫")
				.setTabListener(listener));
		actionBar.addTab(actionBar.newTab().setText("解密")
				.setTabListener(listener));
	}

 类似资料: