2011-10-26 109 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作爲根視圖,它實際上填充了對話窗口。