嗨,我正在尝试做简单的应用程序(媒体播放器)。我有一些问题。我已经完成了一个带有 3 个按钮的菜单(在 menu.xml 文件中),在 MainActivity.java 中完成了 3 个选项卡。我的问题是:如何使这些项目(按钮和选项卡)应用于所有活动?我是否需要将 MainActivity 中的代码粘贴到任何地方?(addTab,覆盖函数等)在我看来必须有其他方式。更重要的是:当我点击tab1时,我想这样做,listview出现在下面,如果我点击tab2,gridview出现在下面。我应该为我的 3 个标签做 3 个不同的布局和 3 个不同的活动吗?还是 1 个布局和 1 个包含所有项目的活动?它应该是什么样子。
我已经这样做了,当我单击 tab1 时,我开始新活动,该新活动的 onCreate 函数使用 listview 加载新布局(用于 tab2 gridview),但在这种情况下,我缺少我的操作栏和选项卡:(当我从复制操作栏和选项卡代码时MainActivity 应用程序甚至没有启动 :( 但是我之前说过,如果我被迫将该代码粘贴到任何地方,那就太奇怪了
我的 menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_home"
android:icon="@drawable/navigation_back"
android:title="@string/action_home"
android:showAsAction="always" />"
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:icon="@drawable/ic_action_overflow"
android:title="@string/action_settings"
android:showAsAction="always" />
</menu>
MainActivity.java
public class MainActivity extends Activity implements ActionBar.TabListener{
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar to show tabs.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText(R.string.title_section_music)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_section_video)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_section_photo)
.setTabListener(this));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current tab position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current tab position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
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_home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.action_search:
//openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
Intent intent = new Intent(this, PlayerListViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/*
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String[] tekst = {"Music", "Video", "Settings", "Exit"};
Toast.makeText(MainActivity.this, "" + tekst[position], Toast.LENGTH_SHORT).show();
if(position == 3)
{
finish();
System.exit(0);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
}