我正在尝试遵循此问题的答案:Cancel Done buttons in Calendar App - Is it part of Action Bar? 我想创建自定义操作栏
我是 Android 新手,所以我的问题是:我必须编写 Fragment 代码吗?我怎样才能做到这一点?我创建了一个方法来设置自定义栏,我在 onCreateView 调用,它不起作用...操作栏没有出现。
private void createCustomActionBar(){
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarButtons = inflater.inflate(R.layout.custom_action_bar,
new LinearLayout(MyActivity.this), false);
View cancelActionView = actionBarButtons.findViewById(R.id.action_cancel);
cancelActionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
processForm();
} catch (Exception e) {
e.printStackTrace();
}
}
});
View doneActionView = actionBarButtons.findViewById(R.id.action_done);
// doneActionView.setOnClickListener();
this.getActionBar().setCustomView(actionBarButtons);
}
我已经从 github 项目复制了 xml、颜色、drawables。
最佳答案
在设置自定义 ActionBar
之前,您似乎缺少一行代码:
this.getActionBar().setDisplayShowCustomEnabled(true);
请参阅documentation .
Set whether a custom view should be displayed, if set.
关于java - 带有完成和取消按钮的自定义操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321841/