How to set the width of a DialogFragment in percentage?

▼魔方 西西 提交于 2019-12-09 17:59:10

问题


I have a DialogFragment with an xml layout and I need it to be, let's say 75% as wide as the screen. When I look for answers I always find the common weight-solution, which does not help me, as my fragment overlays the current activity as shown below and does not take the full screen's size. I also don't want to use "ems" as a fix width as I can't know the user's screen size.

Is there a way (preferably in xml) to set the root LinearLayout's width to said 75%? I think it would be a possible workaround to define a whole set of LinearLayouts and make some of them transparent but this seems to be a rather ugly solution...

If it helps, here is the fragment's layout:

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

回答1:


I don't think it is possible to achieve this without using code.

Anyway I'm using this code inside my custom DialogFragment class. Hope it helps.

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);
}



回答2:


You can achieve this by setting a style to your dialogfragment on it's onCreate method:

setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);

and in your 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>


来源:https://stackoverflow.com/questions/26974068/how-to-set-the-width-of-a-dialogfragment-in-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!