您必须在操作栏中使用自定义布局而不是菜单项来实现此目的。
在onCreate()
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.actionbar_number);
你的布局应该是:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_marginRight="@dimen/marginMedium"
android:gravity="center_vertical"
android:text="0"
android:textColor="@color/actionbar_number"
android:textSize="28dp"
android:textStyle="bold" />
要更新号码:
TextView chapterNumber = (TextView) getSupportActionBar().getCustomView();
chapterNumber.setText(String.valueOf(number));
更新
要添加菜单操作项,您应该能够正常执行此操作,请注意,自定义布局的位置可能会与菜单项重叠或隐藏,如果它们出现在操作栏中。
在menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_share"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
android:icon="@drawable/ic_menu_share"
android:showAsAction="ifRoom"
android:title="@string/share"
android:visible="false"/>
</menu>
然后在你的活动中。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_share);
ShareActionProvider shareActionProvider = (ShareActionProvider) item.getActionProvider(); //line 387
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Test");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");
shareActionProvider.setShareIntent(shareIntent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.menu_share:
// EXAMPLE OF WHAT YOU CAN DO
// Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// sharingIntent.setType("image/png");
// sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(f));
// //sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
// //sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Body");
// startActivity(Intent.createChooser(sharingIntent, "Share via"));
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}