2017-01-18 2 views
1

내 앱에 서브 클래스가 DialogFragment (android.support.v4.app.DialogFragment) 개 있었으므로 오랫동안 정상적으로 작동했습니다. 다이얼로그 자체는 onCreateDialog() 콜백으로 생성되었습니다.Android : support 맞춤 레이아웃이 표시된 DialogFragment가 표시되지 않습니다.

이제 새로운 대화 상자 레이아웃으로 전환하고 완전히 사용자 지정된 대화 상자를 구성하기로 결정했습니다. 최선의 방법은 onCreateDialog()의 재정의를 제거하고 onCreateView() 콜백에서 View 계층을 부 풀리는 것입니다. 나는 그렇게했다.

결과 동작이 매우 이상합니다 - 대화 상자를 표시해야 할 때 화면이 어두워 지지만 대화 상자의 레이아웃이 표시되지 않습니다 ("뒤로"버튼의 동작 또한 대화 상자의 기능과 일치합니다) :

enter image description here

내가 다시 (onCreateDialog() 콜백을 사용) 이전 구현에 복귀하려고 - 그것은 여전히 ​​작동합니다.

제 질문은 : 내가 뭘 잘못하고있는 걸까요?

대화의 코드 :

/** 
* A dialog that can show title and message and has two buttons. User's actions performed 
* in this dialog will be posted to {@link EventBus} as {@link PromptDialogDismissedEvent}. 
*/ 
public class PromptDialog extends BaseDialog { 


    /* package */ static final String ARG_TITLE = "ARG_TITLE"; 
    /* package */ static final String ARG_MESSAGE = "ARG_MESSAGE"; 
    /* package */ static final String ARG_POSITIVE_BUTTON_CAPTION = "ARG_POSITIVE_BUTTON_CAPTION"; 
    /* package */ static final String ARG_NEGATIVE_BUTTON_CAPTION = "ARG_NEGATIVE_BUTTON_CAPTION"; 

    @Inject EventBus mEventBus; 

    private TextView mTxtTitle; 
    private TextView mTxtMessage; 
    private Button mBtnPositive; 
    private Button mBtnNegative; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     getControllerComponent().inject(this); 
    } 

    // THIS CODE MAKES THE SCREEN DIM, BUT THE ACTUAL LAYOUT IS NOT SHOWN 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.dialog_info_prompt, container, false); 

     initSubViews(rootView); 

     populateSubViews(); 

     return rootView; 

    } 

    private void initSubViews(View rootView) { 
     mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title); 
     mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message); 
     mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive); 
     mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative); 

     mBtnPositive.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE)); 
      } 
     }); 

     mBtnNegative.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE)); 
      } 
     }); 
    } 

    private void populateSubViews() { 
     String title = getArguments().getString(ARG_TITLE); 
     String message = getArguments().getString(ARG_MESSAGE); 
     String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION); 
     String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION); 

     mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title); 
     mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message); 
     mBtnPositive.setText(positiveButtonCaption); 
     mBtnNegative.setText(negativeButtonCaption); 
    } 


    // THE BELOW CODE WORKS FINE (the dialog is shown and responsive) 

// @Override 
// public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) { 
//  String title = getArguments().getString(ARG_TITLE); 
//  String message = getArguments().getString(ARG_MESSAGE); 
//  String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION); 
//  String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION); 
// 
//  Dialog dialog = new AlertDialog.Builder(getActivity()) 
//    .setTitle(TextUtils.isEmpty(title) ? "" : title) 
//    .setMessage(TextUtils.isEmpty(message) ? "" : message) 
//    .setPositiveButton(TextUtils.isEmpty(positiveButtonCaption) ? "" : positiveButtonCaption, 
//      new DialogInterface.OnClickListener() { 
//       @Override 
//       public void onClick(DialogInterface dialog, int which) { 
//        mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE)); 
//       } 
//      }) 
//    .setNegativeButton(TextUtils.isEmpty(negativeButtonCaption) ? "" : negativeButtonCaption, 
//      new DialogInterface.OnClickListener() { 
//       @Override 
//       public void onClick(DialogInterface dialog, int which) { 
//        mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE)); 
//       } 
//      }) 
//    .create(); 
// 
//  return dialog; 
// } 

} 

사용자 정의 대화 상자의 레이아웃 :

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

    <TextView 
     android:id="@+id/txt_dialog_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:layout_gravity="center_horizontal" 
     style="@style/AppTheme.TextView.DialogTitle" 
     tools:text="Dialog title"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_marginTop="10dp" 
     android:background="@color/gray"/> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="@dimen/fragment_default_padding"> 

     <TextView 
      android:id="@+id/txt_dialog_message" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      style="@style/AppTheme.TextView.DialogMessage" 
      tools:text="Dialog message very very very very very very very very very very very very long"/> 

     <Button 
      android:id="@+id/btn_dialog_positive" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/button_height" 
      android:layout_marginTop="15dp" 
      android:layout_below="@id/txt_dialog_message" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      style="@style/AppTheme.GreenButton" 
      tools:text="Positive"/> 

     <Button 
      android:id="@+id/btn_dialog_negative" 
      android:layout_width="wrap_content" 
      android:layout_height="@dimen/button_height" 
      android:layout_marginTop="15dp" 
      android:layout_below="@id/txt_dialog_message" 
      android:layout_toStartOf="@+id/btn_dialog_positive" 
      android:layout_toLeftOf="@id/btn_dialog_positive" 
      android:layout_marginEnd="15dp" 
      android:layout_marginRight="15dp" 
      style="@style/AppTheme.GrayButton" 
      tools:text="Negative"/> 

    </RelativeLayout> 

</LinearLayout> 

답변

0

나는이 해결 방법으로 결국,하지만, 난 여전히 WTF 이해하고 싶어 :

// This is a workaround for the strange behavior of onCreateView (which doesn't show dialog's layout) 
    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext()); 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     View dialogView = inflater.inflate(R.layout.dialog_info_prompt, null); 
     dialogBuilder.setView(dialogView); 

     initSubViews(dialogView); 

     populateSubViews(); 

     setCancelable(false); 

     return dialogBuilder.create(); 
    } 

    private void initSubViews(View rootView) { 
     mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title); 
     mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message); 
     mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive); 
     mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative); 

     mBtnPositive.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dismiss(); 
       mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE)); 
      } 
     }); 

     mBtnNegative.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dismiss(); 
       mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE)); 
      } 
     }); 
    } 

    private void populateSubViews() { 
     String title = getArguments().getString(ARG_TITLE); 
     String message = getArguments().getString(ARG_MESSAGE); 
     String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION); 
     String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION); 

     mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title); 
     mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message); 
     mBtnPositive.setText(positiveButtonCaption); 
     mBtnNegative.setText(negativeButtonCaption); 
    } 
+0

가 대규모 성가신 문제 - 이건 진짜 버그 야. – cfl

관련 문제