RES /菜单/ mainmenu.xml
<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"
我没有&#39;我知道它为什么会这样。
答案 0 :(得分:0)
您正在扩展ActionbarActivity
建议您使用支持库中的Actionbar。
更改为
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:logo="@drawable/ic_action_settings"
app:showAsAction="ifRoom|withText"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_logout"
android:logo="@drawable/logout"
app:showAsAction="ifRoom|withText"
android:title="@string/action_logout"/>
</menu>
引用文档
请注意,上面的showAsAction属性使用自定义命名空间 在标签中定义。使用任何XML时都需要这样做 由支持库定义的属性,因为这些属性可以 旧设备上的Android框架中不存在。所以你必须使用 您自己的命名空间作为由...定义的所有属性的前缀 支持图书馆。
答案 1 :(得分:0)
我在这里做了一个愚蠢的错误.. 这是我更改的正确代码
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_settings"
yourapp:showAsAction="always"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_logout"
android:icon="@drawable/logout"
yourapp:showAsAction="always"
android:title="@string/action_logout"/>
</menu>
将android:logo="@drawable/ic_action_settings"
更改为android:icon="@drawable/ic_action_settings"