Android全屏对话框确认和拒绝操作

时间:2014-11-22 06:25:44

标签: android material-design

材质设计中的全屏对话框应在操作栏/工具栏上显示确认和拒绝操作。

Material design fullscreen dialog

我的问题,我该怎么做?

显示对话框:

getFragmentManager().beginTransaction()
    .add(R.id.container, new MyDialogFragment())
    .addToBackStack(null).commit();

我的对话框片段:

public class MyDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.fragment_layout, container, false);
    }
}

1 个答案:

答案 0 :(得分:19)

只需要完成两件事:

  • 更改向上图标
  • 向片段添加菜单

更改图标:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((ActionBarActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
}

添加保存菜单:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.save_menu, menu);
}

R.menu.save_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item
       android:id="@+id/save
       app:showAsAction="always|withText"
       android:title="@string/save"/>
</menu>