【问题标题】:Android DialogFragment Automatic ResizingAndroid DialogFragment 自动调整大小
【发布时间】:2013-07-29 13:49:31
【问题描述】:

我的问题类似于Full Screen DialogFragment in Android,但这个问题并不能完全解决我的问题。

我有一个DialogFragment,我想为其设置最大尺寸,并在 DialogFragment 超出显示范围时自动缩小尺寸。这似乎应该融入操作系统,但我能够提出的唯一解决方案是轮询显示尺寸并在尺寸超出显示范围时手动调整大小。

DialogFragmentonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)中:

    // Display the first screen
    m_rootView = inflater.inflate(R.layout.fragment_popup, container, false);

    // Readjust the size of the dialog fragment if it is too large for the display
    Globals.AdjustSizeOfDialogFragment(getActivity(), container);

调整Globals.java中的代码大小:

// Adjusts the size of a dialog fragment's popup view to ensure that it fits within the display of the current device
public static void AdjustSizeOfDialogFragment(final Activity parentActivity, final View popupRoot)
{
    // If all of our function parameters are valid
    if ((parentActivity != null) && (popupRoot != null))
    {
        // If the dialog's root view has already rendered (has defined layout parameters)
        if (popupRoot.getLayoutParams() != null)
        {
            // Resize the dialog now
            ResizeDialogFragment(parentActivity, popupRoot);
        }
        // Else the view has not finished rendering yet
        else
        {
            // Assign a listener to notify us when it has finished rendering
            popupRoot.addOnLayoutChangeListener(new OnLayoutChangeListener()
            {
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
                {
                    // Remove the layout listener
                    v.removeOnLayoutChangeListener(this);

                    // Now we can render the dialog
                    ResizeDialogFragment(parentActivity, popupRoot);
                }
            });
        }
    }
}

// Does the actual logic/math behind the AdjustSizeOfDialogFragment function
private static void ResizeDialogFragment(Activity parentActivity, View popupRoot)
{
    // Retrieves information about the display of the current device
    DisplayMetrics              deviceDisplayMetrics    = new DisplayMetrics();
    // Layout parameters that need to be applied to the popup's root view
    LinearLayout.LayoutParams   llParams                = null;
    FrameLayout.LayoutParams    flParams                = null;

    // If all of our function parameters are valid
    if ((parentActivity != null) && (popupRoot != null) && (popupRoot.getLayoutParams() != null))
    {
        // Retrieve the layout parameters from the popup's root view and also get information on the current device's display
        if (popupRoot.getLayoutParams() instanceof LinearLayout.LayoutParams)
        {
            llParams = (LinearLayout.LayoutParams) popupRoot.getLayoutParams();
        }
        else if (popupRoot.getLayoutParams() instanceof FrameLayout.LayoutParams)
        {
            flParams = (FrameLayout.LayoutParams) popupRoot.getLayoutParams();
        }
        parentActivity.getWindowManager().getDefaultDisplay().getMetrics(deviceDisplayMetrics);

        // If either the height or width of the popup view is greater then the height or width
        // of the current display, clip them to approximately 90% of the display's width/height.
        if (llParams != null)
        {
            if (llParams.width > deviceDisplayMetrics.widthPixels)
            {
                llParams.width = (int) (deviceDisplayMetrics.widthPixels * 0.9);
            }
            if (llParams.height > deviceDisplayMetrics.heightPixels)
            {
                llParams.height = (int) (deviceDisplayMetrics.heightPixels * 0.9);
            }
        }
        else if (flParams != null)
        {
            if (flParams.width > deviceDisplayMetrics.widthPixels)
            {
                flParams.width = (int) (deviceDisplayMetrics.widthPixels * 0.9);
            }
            if (flParams.height > deviceDisplayMetrics.heightPixels)
            {
                flParams.height = (int) (deviceDisplayMetrics.heightPixels * 0.9);
            }
        }
    }
}

问题是:真正的 Android 方法是什么?这是我唯一的选择吗?这似乎应该内置在操作系统中,但我问过的人都没有找到它。

【问题讨论】:

    标签: java android android-ui android-dialogfragment


    【解决方案1】:

    也许你有这个问题,因为窗口正在调整显示你的DialogFragment,所以如果在显示的活动的AndroidManifest.xml 中将windowSoftInputMode 属性设置为adjustNothing,可以解决你的问题。

    <activity
        ...
        android:windowSoftInputMode="adjustNothing">
    ...
    

    仅供参考:https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2021-11-25
      • 2018-02-06
      • 2015-03-13
      相关资源
      最近更新 更多