定义一个自定义的ActionBar的title,并添加一个overflow的Action Item。
代码实现 如下 :
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.SubMenu; public class MainActivity extends SherlockActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initActionBarTitle(); } private void initActionBarTitle() { // 可以自定义actionbar getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); // 不在actionbar显示logo getSupportActionBar().setDisplayShowHomeEnabled(false); View mainActionBarView = LayoutInflater.from(this).inflate(R.layout.main_action_bar, null); getSupportActionBar().setCustomView(mainActionBarView); } @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { MenuItem search = menu.add(0, 1, 2, "share"); search.setIcon(R.drawable.abs__ic_menu_share_holo_dark); search.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); SubMenu addMenu = menu.addSubMenu(0, 2, 3, "overflow"); addMenu.add(0, 3, 0, "更换主题"); addMenu.add(0, 4, 0, "系统设置"); addMenu.add(0, 5, 0, "修改密码"); addMenu.add(0, 6, 0, "更换头像"); MenuItem overFlowItem = addMenu.getItem(); overFlowItem.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark); overFlowItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "更换主题", Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(MainActivity.this, "系统设置", Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(MainActivity.this, "修改密码", Toast.LENGTH_SHORT).show(); break; case 6: Toast.makeText(MainActivity.this, "更换头像", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } }
效果图 如下 :