是否可以在具有操作栏的 Activity 中添加选项卡主机? 我希望它看起来像这样:
|--------------------------------------|
|< Title ActionBar Settings|
|--------------------------------------|
| |
| |
| |
| |
| Tab 1 Content |
| |
| |
| |
| |
|--------------------------------------|
| Tab 1 | Tab 2 | Tab 3 |
|--------------------------------------|
我不想使用 ActionBar 选项卡的原因是它们不支持底部选项卡。我正在使用底部选项卡,如本问题中所述: Android: Tabs at the BOTTOM
而且我仍然想使用 ActionBar。
这可以做到吗?
编辑:
这是我正在使用的代码
public class TabsFragment extends Fragment {
public TabsFragment() {
}
private TabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(getActivity(), Timeline.TimelineFragment.class);
View view = inflater.inflate(R.layout.activity_tab_host, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content");
tab.setContent(i);
mTabHost.addTab(tab);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
return view;
}
}
和时间线 fragment 。 fragment_timeline.xml 包含普通 View 。
public static class TimelineFragment extends Fragment {
public TimelineFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
return rootView;
}
}
这是启动时调用的 Activity :
public class TabsFragmentActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_fragment);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new TabsFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabs, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这段代码给了我错误:
05-02 20:04:14.525: E/AndroidRuntime(2884): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
05-02 20:04:14.525: E/AndroidRuntime(2884): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
最佳答案
我不明白为什么不。 TabHost 是常规 View 。只要您不需要(已弃用的)TabActivity 将多个 Activity 显示为选项卡,您就可以使用 TabContentFactory 的实例添加选项卡。界面。
一个非常简单的例子如下:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_control_layout);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
for (int i = 0; i < 3; i++)
{
final String tabText = "Content of tab number " + i;
TabSpec spec = tabHost.newTabSpec("tab" + i);
spec.setIndicator("Tab number " + i);
spec.setContent(new TabHost.TabContentFactory()
{
@Override
public View createTabContent(String tag)
{
TextView tabContent = new TextView(MainActivity.this);
tabContent.setText(tabText);
return tabContent;
}
});
tabHost.addTab(spec);
}
}
但是,您应该检查 this .
Don't use bottom tab bars
Other platforms use the bottom tab bar to switch between the app's views. Per platform convention, Android's tabs for view control are shown in action bars at the top of the screen instead. In addition, Android apps may use a bottom bar to display actions on a split action bar.
You should follow this guideline to create a consistent experience with other apps on the Android platform and to avoid confusion between actions and view switching on Android.
关于java - 带有 ActionBar 的 TabHost fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438575/