问题
Fullscreen dialogs in material design should have the confirmation and dismissive actions on the actionbar/toolbar.
My question, how can i do this?
To show the dialog:
getFragmentManager().beginTransaction()
.add(R.id.container, new MyDialogFragment())
.addToBackStack(null).commit();
My dialog fragment:
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:
Only two things need to be done:
- Change the up icon
- Add a menu to the fragment
Changing icon:
@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);
}
Add Save menu:
@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>
来源:https://stackoverflow.com/questions/27074717/android-fullscreen-dialog-confirmation-and-dismissive-actions