【问题标题】:Android: Custom Dialog has wrong dimensionsAndroid:自定义对话框的尺寸错误
【发布时间】:2012-08-19 00:12:13
【问题描述】:

我正在尝试显示一个带有自定义视图的对话框。布局非常简单;基本上有两个LinearLayouts;每个布局都带有 TextViewEditText

我使用的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  <LinearLayout
    android:orientation="vertical"
    android:layout_margin="5dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ll_element1">    
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Element 1"/>
    <EditText
      android:layout_width="250dip"
      android:layout_height="wrap_content"
      android:lines="5"/>
  </LinearLayout>
  <LinearLayout
    android:orientation="vertical"
    android:layout_margin="5dip"
    android:layout_width="wrap_content"
    android:layout_below="@id/ll_element1"
    android:layout_height="wrap_content">    
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Element 2"/>
    <EditText
      android:layout_width="250dip"
      android:layout_height="wrap_content"
      android:lines="5"/>
  </LinearLayout>
</RelativeLayout>

但如果我将其设置为对话框视图,则如下所示:

我显示对话框的代码是这样的:

var view = this.LayoutInflater.Inflate(Resource.Layout.Custom_Dialog, null);

var dialog = new AlertDialog.Builder(this)
            .SetView(view)
            .Create();

dialog.Show();

在我看来,对话框的宽度应该更小,以便布局填充对话框。 我已经尝试了几件事:

  • 使用 LinearLayout 而不是 RelativeLayout
  • 显式设置所有尺寸
  • 使用 Dialog 而不是 AlertDialog

那么,我必须做些什么才能获得正确大小的对话框?

编辑 更新了图片

【问题讨论】:

    标签: android android-layout xamarin.android


    【解决方案1】:

    删除 TextView 并在 EditText 上使用android:hint。将EditText in One LinearLayoutvertical Orientation. 放在一起

    更改 android:layout_width of EditText as fill_parent 及其父级。

    【讨论】:

    • 如果我将EditText的宽度设置为fill_parent,那么EditText就会被拉伸到Dialog的宽度;现在的问题是,对话框非常宽;比我想要的更宽
    【解决方案2】:

    您在EditText 中设置了lines=5,所以它占用了空间。使用dp 而不是dip,检查this

    现在您希望它如何显示?提供一些参考,以便确定确切的问题。

    【讨论】:

    • 身高不是问题;这是宽度。 (我应该在我的问题中明确提到这一点)。 dip 等于 dp。
    • 如果高度不是问题,那么对于宽度,您将设置恒定的 250 倾角。您可以将其设置为较低的值或使用换行内容。
    • 我也尝试设置所有尺寸 explizit,但似乎对话框忽略了它们
    【解决方案3】:

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll_element1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:orientation="vertical" >
    
        <EditText
            android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:hint="Element 1"
            android:lines="5" />
    
        <EditText
            android:layout_width="250dip"
            android:layout_height="wrap_content"
            android:hint="Element 2"
            android:lines="5" />
    
    </LinearLayout>
    

    在你的活动中

    final Dialog dialog = new Dialog(YourActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.yourCustomdialog);
    dialog.setCancelable(true);
    dialog.show();
    

    【讨论】:

    • 相对布局内不需要线性。这是多余的。
    【解决方案4】:

    这些答案都没有帮助我,所以这是我的解决方案(也许有人觉得它有用)。

    NumberPicker 中显示的AlertDialog 中出现了类似的问题。 NumberPicker 有自己的布局,以相对单位("match_parent"android:layout_weight、...)定义。

    在没有rootView 的情况下膨胀布局会导致布局具有默认参数,因此布局大小与预期不符。使用rootView 膨胀布局会导致IllegalStateException:当调用AlertDialog#create() 时,由于create() 方法将视图附加到内部对话框布局,“指定的子级已经有一个父级”

    How to make an alert dialog fill 90% of screen size? 中所述,您可以像这样覆盖默认参数:

    AlertDialog dialog = builder.show();
    
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.copyFrom(dialog.getWindow().getAttributes());
    params.width = 300; // LayoutParams.MATCH_PARENT will match dialog_layout which can be stretched to fullwidth.
    params.height = 300;
    dialog.getWindow().setAttributes(params);
    

    您可以创建dialog_helper.xml,而不是使用以像素为单位的固定宽度和高度,您可以在其中以相对单位 (android:id="@+id/dialog_dimension") 定义一些透明视图,稍后您将从中复制宽度和高度。然后将此布局包含到活动/片段布局中:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout ...>
        <LinearLayout ....>
            ...
        </LinearLayout>
        <include layout="@layout/dialog_helper"/>
    </RelativeLayout>
    

    由于此视图包含在活动布局中,因此它具有“测量”的宽度和高度,可用作对话框参数:

    View dialogDim = findViewById(R.id.dialog_dimension);
    params.width = dialogDim.getMeasuredWidth();
    params.height = dialogDim.getMeasuredHeight();
    

    dialog.getWindow().getAttributes() 计算相对大小将不起作用,因为它包含一些负值常量(MATCH_PARENT 或其他)。因此,如果您想计算相对于显示大小的对话框大小,请使用screen metrics

    注意,屏幕旋转后将使用相同的宽度和高度,因此我建议将您的对话框包装到DialogFragment,当设备配置更改时会自动重新创建,因此对话框将使用更新的dialogDim创建根据当前屏幕方向。这是一个简单的例子How to create DialogFragment

    上一个例子包含activity和fragment之间的通信(activity需要接收关于对话事件的消息)。这需要定义自定义侦听器接口 + 正确覆盖 Fragment#onAttach() 方法。所有这些步骤都在以下文章中进行了解释:Cummunication between Fragment and Activity

    【讨论】:

      【解决方案5】:

      使用builder并在显示时添加宽度和高度

      builder.show().getWindow().setLayout(600, 250);
      

      【讨论】:

        猜你喜欢
        • 2015-08-10
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        相关资源
        最近更新 更多