我最近实现了一个使用ActionBar.Tabs的简单项目。主要活动有这个实现:
public class MainActivity extends Activity {
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new Tab1Fragment();
Fragment fragmentTab2 = new Tab2Fragment();
Fragment fragmentTab3 = new Tab3Fragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSearch() {
//
}
public void openSettings() {
//
}
}
与此活动相关联的布局是:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
当我在手机上运行项目时,标签位于屏幕顶部,位于actiobBar本身下方。我想把这个标签放在屏幕的底部。任何人都可以告诉我一种方法吗?
ps:我实际上是尝试使用TabHost,TabWidget和Fragments的布局,但我收到了这里描述的错误:Android device can't instantiate launcher activity