android fragment大小,Android DialogFragment自动调整大小

我有一个DialogFragment,我想设置最大尺寸,并自动调整大小,如果DialogFragment延伸超过显示的范围。这似乎是应该被烘焙到操作系统中的东西,但我已经能够提出的唯一解决方案是轮询显示尺寸,并在尺寸超出显示范围时手动调整尺寸。

在DialogFragment的onCreateView(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的方式来做到这一点?这是我唯一的选择吗?这似乎是应该构建到操作系统中的东西,但我没有问过任何人能够找到它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值