java 如果您想让DialogFragment具有嵌入能力,则必须在布局中定义对话框的UI,然后在onCreateView()回调中加载布局。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 如果您想让DialogFragment具有嵌入能力,则必须在布局中定义对话框的UI,然后在onCreateView()回调中加载布局。相关的知识,希望对你有一定的参考价值。

<!-- Large screen boolean values -->
<resources>
    <bool name="large_layout">true</bool>
</resources>
<!--以下两个版本的布尔资源适用于不同的屏幕尺寸:-->
<!-- Default boolean values -->
<resources>
    <bool name="large_layout">false</bool>
</resources>
/*以下代码可根据屏幕尺寸决定将片段显示为对话框还是全屏 UI:*/
boolean mIsLargeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
}

public void showDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    CustomDialogFragment newFragment = new CustomDialogFragment();
    /*mIsLargeLayout 布尔值指定当前设备是否应该使用应用的大布局设计
    (进而将此片段显示为对话框,而不是全屏显示)。 
    设置这种布尔值的最佳方法是声明一个布尔资源值,
    其中包含适用于不同屏幕尺寸的备用资源值。*/
    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();
    }
}
/*以下示例 DialogFragment 可以显示为对话框或嵌入式片段
(使用名为 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;
    }
}

以上是关于java 如果您想让DialogFragment具有嵌入能力,则必须在布局中定义对话框的UI,然后在onCreateView()回调中加载布局。的主要内容,如果未能解决你的问题,请参考以下文章

iOS 自动布局 - 当您想让视图拉伸到其父级的完整大小时,正确的方法是啥?

java dialogfragment的一些常用的样式

java DialogFragment_Sample

java 在DialogFragment内管理的基础AlertDialog

java 在DialogFragment内管理的基础AlertDialog

java 在DialogFragment内管理的基础AlertDialog