RES /菜單/ mainmenu.xml爲什麼我ActionButton將不會出現在我的動作條
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:logo="@drawable/ic_action_settings"
android:showAsAction="ifRoom|withText"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_logout"
android:logo="@drawable/logout"
android:showAsAction="ifRoom|withText"
android:title="@string/action_logout"/>
</menu>
MainMenu.java 公共類MainMenu的擴展ActionBarActivity {
Button userinfo, requestservice, makepayment, trackparcel, checkcard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
// Show the Up button in the action bar.
// setupActionBar();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
userinfo = (Button) findViewById(R.id.userinfo);
requestservice = (Button) findViewById(R.id.requestservice);
makepayment = (Button) findViewById(R.id.makepayment);
trackparcel = (Button) findViewById(R.id.usertrackbutton);
checkcard = (Button) findViewById(R.id.checkcard);
userinfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, UserInfo.class);
startActivity(intent);
}
});
requestservice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, RequestService.class);
startActivity(intent);
}
});
makepayment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, Payment.class);
startActivity(intent);
}
});
trackparcel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, TrackParcel.class);
startActivity(intent);
}
});
checkcard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainMenu.this, CheckCard.class);
startActivity(intent);
}
});
}
public void onBackPressed() {
new AlertDialog.Builder(MainMenu.this)
.setTitle("Logout")
.setMessage("Would you like to logout?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent(MainMenu.this,
Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(MainMenu.this);
Editor edit = sp.edit();
edit.clear();
edit.commit();
startActivity(intent);
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// user doesn't want to logout
}
}).show();
}
@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, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_logout: {
new AlertDialog.Builder(MainMenu.this)
.setTitle("Logout")
.setMessage("Would you like to logout?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent(MainMenu.this,
Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(MainMenu.this);
Editor edit = sp.edit();
edit.clear();
edit.commit();
startActivity(intent);
finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// user doesn't want to logout
}
}).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
我想問的是那爲什麼我的動作項目不會出現在我的ActionBar事件中,儘管我已經寫過android:showAsAction="ifRoom|withText"
或android:showAsAction="always"
我不知道爲什麼它的行爲如此。
哇這是偉大正確的代碼。但我希望圖標出現在actionBar而不是文本中 –
@Jiazzyuser文檔中的另一個引用如果菜單項同時提供標題和圖標 - 標題和圖標屬性 - 則操作項僅顯示圖標默認。如果要顯示文本標題,請將「withText」添加到showAsAction屬性中。例如:' ' –
Raghunandan
還注意到如果沒有足夠的空間它會出現在溢出菜單中 – Raghunandan