安卓:自定义对话框中有错误的尺寸 [英] Android: Custom Dialog has wrong dimensions

查看:156
本文介绍了安卓:自定义对话框中有错误的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示与自定义视图的对话框。布局是pretty简单; basicly有两个 LinearLayouts ;每一个布局与的TextView 的EditText

I'm trying to display a dialog with a custom view. The Layout is pretty simple; basicly there are two LinearLayouts; every Layout with a TextView and an EditText.

我使用的布局是这样的:

The Layout i'm using is this:

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

但是,如果我把它设置为视图对话框如下所示:

But if i set it as view of a dialog the looks like this:

我的code,显示对话框是这样的:

My Code to show the Dialog is this:

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

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

dialog.Show();

在该对话框的我看来宽度,这样的布局填充对话框应较小。
我试过几件事情:

In my opinion width of the Dialog should be smaller so that the Layout fills the dialog. I've tried several things:


  • 使用,而不是一个RelativeLayout的的LinearLayout

  • 设置各种规模explizit

  • 二手对话,而不是AlertDialog

那么,我有什么做的就是正确大小的对话框?

So, what do i have to do to get a correctly sized dialog?

修改
更新了图片

edit Updated the Picture

推荐答案

试试这个

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

这篇关于安卓:自定义对话框中有错误的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆