2011-10-26 101 views
1

使用onCreateView一直试图弄清楚这一点,现在一会儿:自定义视图在DialogFragment

我试图创建一个使用在Dev site所示的标准方法的自定义对话框。

public class CustomDialog extends DialogFragment {  
    private OnDialogResultListener mOnDialogResultListener = null; 
    public void setOnDialogResultListener(OnDialogResultListener dialogResultListener) { 
     mOnDialogResultListener = dialogResultListener; 
    } 

    public static CustomDialog newInstance(OnDialogResultListener dialogResultListener) { 
     CustomDialog frag = new CustomDialog();   
     frag.setOnDialogResultListener(dialogResultListener); 
     return frag; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     getDialog().setTitle(getString(R.string.Dialog_CustomDialog_Title)); 
     View v = inflater.inflate(R.layout.customdialog, container, false); 
     return v; 
    } 
} 

随着XML之中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" android:background="@color/white" android:layout_margin="0px" android:layout_width="fill_parent"> 
    <EditText android:hint="@string/Dialog.CustomDialog.OldPassword" android:layout_marginBottom="@dimen/normalMargin" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/EditText02" android:layout_width="fill_parent"></EditText> 
    <EditText android:hint="@string/Dialog.CustomDialog.NewPassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/EditText01" android:layout_width="fill_parent"></EditText> 
    <EditText android:hint="@string/Dialog.CustomDialog.RetypePassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/editText1" android:layout_width="fill_parent"> 
     <requestFocus></requestFocus> 
    </EditText> 
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent"> 
     <Button android:layout_height="wrap_content" android:text="@string/save" android:layout_width="@dimen/normalButtonWidth" android:id="@+id/btn_Custom_save"></Button> 
     <Button android:layout_height="wrap_content" android:text="@string/cancel" android:layout_width="@dimen/normalButtonWidth" android:id="@+id/btn_Custom_cancel"></Button> 
    </LinearLayout> 
</LinearLayout> 

以及创建和显示对话框后,我留下了: enter image description here

白色背景已应用于强调意外和不想要的行为。

任何想法?我试过改变宽度/高度,在水平LinearLayout中使用权重,以编程方式设置宽度/高度 - 都无济于事。

回答

1

我想出了一个便宜的修复程序,我宁愿不必使用它。

在布局的任何位置添加一个0px高度,2000dp(某些大数字)的宽度视图会导致它填充对话框。

0

使用DialogFragment的宽度和高度为空。用填充父项创建一个额外的子视图组,它应该适合你。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

     <LinearLayout android:orientation="vertical" 
    android:layout_height="fill_parent" android:background="@color/white" android:layout_margin="0px" android:layout_width="fill_parent"> 

.....

+0

我会检查一次,我得到它,并给你信用,如果它的工作:) –

0

如果设置attachToRoot =真inflater.inflate期间应正确显示对话框。

View v = inflater.inflate(R.layout.customdialog, container, true); 
+0

我会尝试测试这个(和Sra的)并相应地标记一个答案 - 问题是我知道更长的时间有访问到我正在使用的代码。但我简单的测试不应该花太长时间来拼凑在一起。 –

2

我发现了这个窍门:。 getDialog()getWindow()setBackgroundDrawableResource(R.color.white);

看起来更好的解决方案。

0

我在我的情况下使用了一个relativeLayout作为根视图,它实际上填充了对话窗口。