Android layout 如何设置DialogFragment的宽度(百分比)?

Android layout 如何设置DialogFragment的宽度(百分比)?,android-layout,android-dialogfragment,Android Layout,Android Dialogfragment,我有一个带有xml布局的DialogFragment,我需要它的宽度,比如说屏幕宽度的75%。当我寻找答案时,我总是会找到常见的权重-解决方案,这对我没有帮助,因为我的片段覆盖了当前活动,如下图所示,并且没有全屏大小。我也不想使用“ems”作为固定宽度,因为我不知道用户的屏幕大小 是否有一种方法(最好是xml)将根线性布局的宽度设置为75%?我认为这是一个可能的解决办法,可以定义一整套线性布局并使其中一些透明,但这似乎是一个相当丑陋的解决方案 如果有帮助,下面是片段的布局: <?xml

我有一个带有xml布局的
DialogFragment
,我需要它的宽度,比如说屏幕宽度的75%。当我寻找答案时,我总是会找到常见的
权重
-解决方案,这对我没有帮助,因为我的片段覆盖了当前活动,如下图所示,并且没有全屏大小。我也不想使用“ems”作为固定宽度,因为我不知道用户的屏幕大小

是否有一种方法(最好是xml)将根
线性布局的宽度设置为75%?我认为这是一个可能的解决办法,可以定义一整套
线性布局
并使其中一些透明,但这似乎是一个相当丑陋的解决方案

如果有帮助,下面是片段的布局:

<?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>

我认为不使用代码是不可能做到这一点的

无论如何,我在自定义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);
}

可以通过在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>

@可绘制/某些背景
@空的
假的
75%
75%
真的

Awsome!这是我上个小时读到的所有答案的组合^_^在
onCreateView()
中添加了相同的代码,但什么也没发生。仅在将其添加到
onResume()
后才起作用。为Android干杯!!在android studio 4.2 C15 SDK30上对我有效在android studio 4.2 C15 SDK30上对我无效