我在ActionBar中有3个按钮。一个在左边并使用它 将另一项活动称为抽屉。另外两个按钮在右边 Actionbar的一面但他们没有工作。
这是我的菜单文件: -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.bottomtabs.FirstActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/like"/>
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/search"
android:title="menu_settings"/>
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/notifications"
android:showAsAction="always"/>
</menu>
这是我正在使用它的活动。
public class FirstActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
String getselectevalue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
getActionBar().setDisplayHomeAsUpEnabled(true);
// mTabHost = new FragmentTabHost(this);
// mTabHost.setup(this, getSupportFragmentManager(),
// R.id.menu_settings);
getselectevalue = getIntent().getExtras().getString("selectedVlaue");
Log.d("got the value here", getselectevalue);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
Bundle b = new Bundle();
b.putString("key", "Simple");
mTabHost.addTab(mTabHost.newTabSpec("Post").setIndicator("Post"),Fragment1.class, b);
//
//mTabHost.addTab(mTabHost.newTabSpec("Post").setIndicator("", getResources().getDrawable(R.drawable.post_selected),FirstActivity.class));
b = new Bundle();
System.out.print("hello git");
b.putString("key", "Contacts");
mTabHost.addTab(mTabHost.newTabSpec("Profile").setIndicator("Profile"),Fragment2.class, b);
b = new Bundle();
b.putString("key", "Custom");
mTabHost.addTab(mTabHost.newTabSpec("Chat").setIndicator("Chat"),Fragment3.class, b);
b = new Bundle();
b.putString("key", "Custom");
mTabHost.addTab(mTabHost.newTabSpec("Event").setIndicator("Event"),Fragment3.class, b);
b = new Bundle();
b.putString("key", "Custom");
mTabHost.addTab(mTabHost.newTabSpec("Shop").setIndicator("Shop"),Fragment3.class, b);
// setContentView(mTabHost);
if(getselectevalue.equalsIgnoreCase("0")){
mTabHost.setCurrentTab(0);
}else{
mTabHost.setCurrentTab(1);
}
}
@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;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
Intent slide = new Intent(FirstActivity.this,Slide.class);
startActivity(slide);
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
break;
case R.id.action_settings:
Toast.makeText(this, "plzzzz start working", Toast.LENGTH_LONG).show();
break;
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.action_settings).setEnabled(true);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
}
请帮我解决一下。非常感谢。
答案 0 :(得分:0)
将 onMenuItemSelected()
重命名为onOptionsItemSelected()
。
使用onMenuItemSelected()
方法替换onOptionsItemSelected()
。除了重命名方法之外,还需要删除int featureId
参数。
答案 1 :(得分:0)
最初这个过于复杂,无法理解,但经过简短的介绍 与@BHARGAV和@COMMONSWARE的讨论,我试着理解了 Actionbar和Menu的行为。我在代码中做了一些更改 开始像我一样的魅力。
以下是我在代码中所做的更改。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
Intent slide = new Intent(FirstActivity.this,Slide.class);
startActivity(slide);
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
return true;
case R.id.menu_settings:
Toast.makeText(this, "plzzzz start working", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.menu_settings).setEnabled(true);
return super.onPrepareOptionsMenu(menu);
}