【发布时间】:2013-01-28 04:30:17
【问题描述】:
我正在努力让一个简单的 tabhost 与 ABS 一起工作,似乎 Android 在尝试“改进”它们时只会让事情变得更加复杂。我正在考虑将我的项目转换为在标签中使用片段,但事实证明,对于我在每个标签中使用的复杂布局来说,这是一种方式。我现在只需要一个普通的 ol 工作 tabhost w。工作腹肌吧。
package com.abs.tabs.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.buhz.feeds.Buhdz;
import com.buhz.login.R;
import com.buhzhyve.localz.Localz;
import com.buhzhyve.trails.Trails;
import com.buhzhyve.trendz.Trendz;
public class FragmentTabs extends SherlockActivity {
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
actionBar=getSupportActionBar();
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost =(TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Do the same for the other tabs
intent = new Intent().setClass(this, Buhdz.class);
spec = tabHost.newTabSpec("Buhdz").setIndicator("Feeds",
res.getDrawable(R.drawable.buhdz_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("Localz").setIndicator("Locals",
res.getDrawable(R.drawable.locals_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Trendz.class);
spec = tabHost.newTabSpec("Trendz").setIndicator("Trends",
res.getDrawable(R.drawable.trends_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Localz.class);
spec = tabHost.newTabSpec("convos").setIndicator("Convos",
res.getDrawable(R.drawable.convos_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Trails.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Trails").setIndicator("Trails",
res.getDrawable(R.drawable.trails_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(4);
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
return true;
case R.id.subItem1:
Toast.makeText(this, "Sub Menu item 1 tapped", Toast.LENGTH_SHORT).show();
return true;
case R.id.subItem2:
Toast.makeText(this, "Sub Menu item 2 tapped", Toast.LENGTH_SHORT).show();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
我不明白你的问题,但你知道你不能混合actionbarsherlock和原生actionbar?
-
我一直想要的只是一个工作的 ABS 操作栏和工作选项卡。如果我能侥幸使用我的旧标签 w。活动 id 很酷(我的标签代码几乎与此相同:androidhive.info/2011/08/android-tab-layout-tutorial)如果我必须学习并为我的标签使用片段,那么我需要帮助将活动转换为片段。
标签: android android-fragments android-activity actionbarsherlock android-tabhost