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

标签 android-layout android-dialogfragment

我有一个 DialogFragment使用 xml 布局,我需要它,假设宽度为屏幕的 75%。当我寻找答案时,我总能找到共同点 weight -解决方案,这对我没有帮助,因为我的片段覆盖了当前事件,如下所示,并且不占用全屏的大小。我也不想使用“ems”作为固定宽度,因为我不知道用户的屏幕尺寸。

enter image description here

有没有办法(最好在 xml 中)设置根 LinearLayout的宽度要说75%?我认为定义一整套 LinearLayout 是一种可能的解决方法。 s并使其中一些透明,但这似乎是一个相当丑陋的解决方案......

如果有帮助,这里是片段的布局:

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

关于android-layout - 如何以百分比设置 DialogFragment 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974068/

相关文章:

android - 是什么导致我的 Android EditText 消失

android - 使用RelativeLayout构建像ui这样的网格时出现问题

android - 为什么我的 DialogFragment 没有显示自定义布局?

java - 对话框 fragment 定位

android - "android:layout_column"属性是什么?

android - Textview 边框和 drawableLeft 之间的填充

android - 显示 DialogFragment 时保持沉浸式模式

android - 如何使用 Dialogfragment 和 ArrayAdapter 创建列表对话框来填充 Java 类中的项目?

java - 如何在Android上与2个对话框 fragment 生命周期进行交互?

java - android - 动态添加按钮到自定义对话框