Showing a Dialog Fullscreen or as an Embedded Fragment
2017-07-24 19:52
471 查看
You might have a UI design in which you want a piece of the UI to appear as a dialog in some situations, but as a full screen or embedded fragment in others (perhaps depending on whether the device is a large
screen or small screen). The
still behave as an embeddable
However, you cannot use
to build the dialog in this case.
If you want the
then load the layout in the
Here's an example
(using a layout named
And here's some code that decides whether to show the fragment as a dialog or a fullscreen UI, based on the screen size:
For more information about performing fragment transactions, see the Fragments guide.
In this example, the
declare a bool resource value with an alternative
resourcevalue for different screen sizes. For example, here are two versions of the bool resource for different screen sizes:
res/values/bools.xml
res/values-large/bools.xml
Then you can initialize the
a dialog when on large screens. Which approach you choose depends on your app design, but showing an activity as a dialog is often useful when your app is already designed for small screens and you'd like to improve the experience on tablets by showing
a short-lived activity as a dialog.
To show an activity as a dialog only when on large screens, apply the
to the
For more information about styling your activities with themes, see the Styles and Themes guide.
From:https://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog
screen or small screen). The
DialogFragmentclass offers you this flexibility because it can
still behave as an embeddable
Fragment.
However, you cannot use
AlertDialog.Builderor other
Dialogobjects
to build the dialog in this case.
If you want the
DialogFragmentto be embeddable, you must define the dialog's UI in a layout,
then load the layout in the
onCreateView()callback.
Here's an example
DialogFragmentthat can appear as either a dialog or an embeddable fragment
(using a layout named
purchase_items.xml):
public class CustomDialogFragment extends DialogFragment { /** The system calls this to get the DialogFragment's layout, regardless of whether it's being displayed as a dialog or an embedded fragment. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout to use as dialog or embedded fragment return inflater.inflate(R.layout.purchase_items, container, false); } /** The system calls this only when creating the layout in a dialog. */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // The only reason you might override this method when using onCreateView() is // to modify any dialog characteristics. For example, the dialog includes a // title by default, but your custom layout might not need it. So here you can // remove the dialog title, but you must call the superclass to get the Dialog. Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); return dialog; } }
And here's some code that decides whether to show the fragment as a dialog or a fullscreen UI, based on the screen size:
public void showDialog() { FragmentManager fragmentManager = getSupportFragmentManager(); CustomDialogFragment newFragment = new CustomDialogFragment(); if (mIsLargeLayout) { // The device is using a large layout, so show the fragment as a dialog newFragment.show(fragmentManager, "dialog"); } else { // The device is smaller, so show the fragment fullscreen FragmentTransaction transaction = fragmentManager.beginTransaction(); // For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // To make it fullscreen, use the 'content' root view as the container // for the fragment, which is always the root view for the activity transaction.add(android.R.id.content, newFragment) .addToBackStack(null).commit(); } }
For more information about performing fragment transactions, see the Fragments guide.
In this example, the
mIsLargeLayoutboolean specifies whether the current device should use the app's large layout design (and thus show this fragment as a dialog, rather than fullscreen). The best way to set this kind of boolean is to
declare a bool resource value with an alternative
resourcevalue for different screen sizes. For example, here are two versions of the bool resource for different screen sizes:
res/values/bools.xml
<!-- Default boolean values --> <resources> <bool name="large_layout">false</bool> </resources>
res/values-large/bools.xml
<!-- Large screen boolean values --> <resources> <bool name="large_layout">true</bool> </resources>
Then you can initialize the
mIsLargeLayoutvalue during the activity's
onCreate()method:
boolean mIsLargeLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIsLargeLayout = getResources().getBoolean(R.bool.large_layout); }
Showing an activity as a dialog on large screens
Instead of showing a dialog as a fullscreen UI when on small screens, you can accomplish the same result by showing anActivityas
a dialog when on large screens. Which approach you choose depends on your app design, but showing an activity as a dialog is often useful when your app is already designed for small screens and you'd like to improve the experience on tablets by showing
a short-lived activity as a dialog.
To show an activity as a dialog only when on large screens, apply the
Theme.Holo.DialogWhenLargetheme
to the
<activity>manifest element:
<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >
For more information about styling your activities with themes, see the Styles and Themes guide.
From:https://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog
相关文章推荐
- Android中如何让DialogFragment全屏(DialogFragment fullscreen)
- Android中如何让DialogFragment全屏(DialogFragment fullscreen)
- Android DialogFragment fullscreen 解决方法
- Android中如何让DialogFragment全屏(DialogFragment fullscreen)
- Android中如何让DialogFragment全屏(DialogFragment fullscreen)
- Android showing LoadingView or EmptyView for Activity or Fragment
- xx can be an out parameter, so you must declare it as in, out or inout.
- Fragment Or DialogFragment Can not perform this action after onSaveInstanceState
- 'telnet' is not recognized as an internal or external command 解决办法
- Defining <uniqueKey> as a "long" or "int" type throws an error on startup
- Dialog frame as an ActiveX control.
- Pocket PC Dialog Full Screen
- 用命令连接mysql , 出现'mysql' is not recognized as an internal or external command错误解决
- AIDL 编译报can be an out parameter, so you must declare it as in, out or inout原因探究
- “NODE_ENV” is not recognized as an internal or external command
- 'powershell' is not recognized as an internal or external command, operable program or batch file
- Pocket PC Dialog Full Screen
- Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.init
- Should PHP run as a CGI script or as an Apache module?
- 'telnet' is not recognized as an internal or external command