如何以百分比设置DialogFragment的宽度?

13

我有一个带有xml布局的DialogFragment,我需要它的宽度只占屏幕的75%。当我寻找答案时,我经常会发现普遍的weight解决方案,并不能帮助我,因为我的片段覆盖了当前活动,如下图所示,并没有占满整个屏幕的大小。我也不想使用“ems”作为固定宽度,因为我无法知道用户的屏幕尺寸。

enter image description here

有没有一种方法(最好是在xml中)来将根LinearLayout的宽度设置为所需的75%?我认为定义一组完整的LinearLayout并使其中一些透明可能是一种解决方法,但这似乎是一种相当丑陋的解决方案...

如果有用的话,这是片段的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_height   = "wrap_content"
    android:layout_width    = "match_parent"
    android:orientation     = "vertical" >

    <EditText
        android:hint            = "@string/inputhint_feedbackName"
        android:id              = "@+id/input_feedbackName"
        android:imeOptions      = "actionNext"
        android:inputType       = "textPersonName"
        android:layout_height   = "wrap_content"
        android:layout_width    = "match_parent"
        android:lines           = "1"
        android:maxLines        = "1"
        android:singleLine      = "true" >
        <requestFocus />
    </EditText>

    <EditText
        android:gravity             = "top"
        android:id                  = "@+id/input_feedbackMessage"
        android:imeOptions          = "actionDone"
        android:layout_height       = "wrap_content"
        android:layout_marginBottom = "20dp"
        android:layout_width        = "match_parent"
        android:lines               = "10"
        android:inputType           = "textMultiLine" />

    <Button
        android:id                  = "@+id/button_feedbackSend"
        android:layout_height       = "wrap_content"
        android:layout_width        = "match_parent"
        android:text                = "@string/label_go"
        android:textSize            = "16sp"
        android:textStyle           = "bold" />
</LinearLayout>
4个回答

63

我认为在不使用代码的情况下是不可能实现这一点的。

无论如何,我正在我的自定义DialogFragment类中使用此代码。希望它有所帮助。

public void onResume() {
    super.onResume();

    Window window = getDialog().getWindow();
    Point size = new Point();

    Display display = window.getWindowManager().getDefaultDisplay();
    display.getSize(size);

    int width = size.x;

    window.setLayout((int) (width * 0.75), WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
}

太棒了!这结合了我过去一个小时阅读的所有答案!^_^ - Rami Alloush
1
onCreateView()中添加了相同的代码,但没有任何反应。只有在将其添加到onResume()后才起作用。为安卓干杯!! - iCantC
在Android Studio 4.2 C15 SDK30上对我有效。 - Miguel Tomás

9
您可以通过在DialogFragment的onCreate方法中设置样式来实现此目的:
setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);

在你的styles.xml中。
   <style name="yourStyle" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/some_background</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowMinWidthMajor">75%</item>
        <item name="android:windowMinWidthMinor">75%</item>
        <item name="android:windowNoTitle">true</item>
    </style>

在Android Studio 4.2 C15 SDK30上,它对我不起作用。 - Miguel Tomás

0

更新代码 - 2021 - kotlin


重写您的对话框片段中的onResume()方法,如下所示:
override fun onResume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val windowMetrics = 
        requireActivity().windowManager.currentWindowMetrics
        val insets: Insets = windowMetrics.windowInsets
            .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
        val width = windowMetrics.bounds.width() - insets.left - 
        insets.right
        val height = windowMetrics.bounds.height() - insets.top - 
        insets.bottom
        val window = dialog!!.window
        if (window != null) {
            window.setLayout((width * 0.90).toInt(), (height * 
            0.90).toInt()) // for width and height to be 90 % of screen
            window.setGravity(Gravity.CENTER)
        }
        super.onResume()
    } else {
        val window = dialog!!.window
        val size = Point()
        // Store dimensions of the screen in `size`
        val display = window!!.windowManager.defaultDisplay
        display.getSize(size)
        // Set the width of the dialog proportional to 90% of the screen width
        window.setLayout((size.x * 0.90).toInt(), 
        ViewGroup.LayoutParams.WRAP_CONTENT)
        window.setGravity(Gravity.CENTER)
        // Call super onResume after sizing
        super.onResume()
    }
}

0
你可以重写onStart方法,并将这段代码添加到这个代码块中。
        getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接