actionbar实现左上角实现保存功能
1、先新建一个activity来继承ActionBarActivity,然后在进行基础设置;
/**
* ActionBar基础activity,进行统一的属性设置
* 所有顶部带有actionbar的类都继承此类。
*/
public class BaseActionBarActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 设置窗口风格为顶部显示Actionbar
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true); //决定是否有title标题头
actionBar.setDisplayHomeAsUpEnabled(true); // 决定左上角图标的右侧是否有向左的小箭头, true
// 有小箭头,并且图标可以点击
actionBar.setDisplayShowHomeEnabled(false);
// 使左上角图标是否显示,如果设成false,则没有程序图标,仅仅就个标题,
// 否则,显示应用程序图标,对应id为android.R.id.home,对应ActionBar.DISPLAY_SHOW_HOME
// force use of overflow menu on devices with menu button
// 在actionbar中显示溢出菜单选项
// http://stackoverflow.com/questions/9286822/how-to-force-use-of-overflow-menu-on-devices-with-menu-button
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField(sHasPermanentMenuKey);
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:// 点击返回图标事件
//这里加上保存事件拦截
saveContent(false);
if (fromWidget == 1) {
startActivity(new Intent(NoteEditorActivity.this, NoteActivity.class));
}
hideKeyBoard();
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
}
}
然后对应的styles里面设置先左箭头替换为保存图片标记,替换的styles.xml找到对应activity对应的styles然后
添加:
<item name="android:homeAsUpIndicator">@drawable/ic_done_holo_lights</item><!--对应的图片-->
原文只是设置为了返回按钮,进行添加变为保存按钮
原文