我正在使用 ActionBarsherlock 库在操作栏中使用选项卡。选择选项卡时如何在活动之间切换。我的代码如下
public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DetailFragment fragment = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailFragment);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 5; i++)
{
ImageView myMediaImg;
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setCustomView(R.layout.tab);
tab.setTabListener(this);
if(i==1)
{
myMediaImg = (ImageView) tab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.ic_launcher);
tab.setTag("tab1");
}
if(i==2)
{
myMediaImg = (ImageView) tab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.ic_launcher);
tab.setTag("tab2");
}
if(i==3)
{
myMediaImg = (ImageView) tab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.ic_launcher);
tab.setTag("tab3");
}
if(i==4)
{
myMediaImg = (ImageView) tab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.ic_launcher);
tab.setTag("tab4");
}
if(i==5)
{
myMediaImg = (ImageView) tab.getCustomView().findViewById(R.id.sk_abs__tab_icon);
myMediaImg.setImageResource(R.drawable.ic_launcher);
tab.setTag("tab5");
}
getSupportActionBar().addTab(tab);
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getTag().equals("tab1"))
{
Intent intent = new Intent(Activity1.this,Activity1.class);
startActivity(intent);
}
if(tab.getTag().equals("tab2"))
{
Intent intent =new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
if(tab.getTag().equals("tab3"))
{
Intent intent = new Intent(Activity1.this,Activity3.class);
startActivity(intent);
}
if(tab.getTag().equals("tab4"))
{
Intent intent = new Intent(Activity1.this,Activity4.class);
startActivity(intent);
}
if(tab.getTag().equals("tab5"))
{
Intent intent = new Intent(Activity1.this,Activity5.class);
startActivity(intent);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
当我运行应用程序 Activity1 反复切换(从 logcat 获得)。