2017-12-27 20 views
0

사용자 정의 AlertDialog Builder 및 alertdialog에서 제목 및 메시지의 글꼴을 변경해야하지만 다음과 같은 방법으로이를 수행 할 수 없습니다.사용자 정의 AlertDialog 빌더 작성 및 제목 및 메시지의 글꼴 변경


CustomAlertDialogBuilder.java :

public class CustomAlertDialogBuilder extends AlertDialog.Builder { 

    public CustomAlertDialogBuilder(Context context) { 
     super(context); 
     TextView title = (TextView) create().getWindow().findViewById(R.id.alertTitle); 
     TextView message = (TextView) create().getWindow().findViewById(android.R.id.message); 

     myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/my_font.ttf"); 
     title.setTypeface(myTypeface); 
     message.setTypeface(myTypeface); 
    } 
} 


가리키고 텍스트 뷰의이 null입니다. TextViews는 어떻게 정의합니까? 나는 초보자입니다, 제발 alertdialog의 폰트를 사용자 정의 alertdialog로 바꾸도록 도와주세요.

답변

0

사용자 지정 대화 상자를 자주 사용하므로 DialogFragment을 사용합니다. 이 대화 상자에는 "확인"및 "취소"버튼이 있습니다. 필요하지 않은 버튼은 제거 할 수 있습니다.

사용자 지정 DialogFragment "fragment_submit_cancel_dialog"에 대한 XML 레이아웃을 만들어야합니다. 자신의 디자인을 만들 수 있기 때문에 대화 상자의 모양에 많은 유연성을 제공합니다.

implements OkCancelDialogFragment.OkCancelDialogListener{ 

청취자 방법 추가 :

@Override 
public void onFinishOkCancelDialog(boolean submit) { 
    if(submit){ 
     // Do what you need here 
    } 
} 

콜이 같은 DialogFragment :

private void startOkDialog(){ 
    String title = "What ever you want as a Title"; 
    String mess = "Your Message!"; 
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess); 
    show(getFragmentManager(), "OkDialogFragment"); 
} 
을 당신이이 DialogFragment이를 추가해야합니다 호출 활동에

이제 사용자 지정 대화 상자 조각의 코드 :

public class OkCancelDialogFragment extends DialogFragment { 

    private static final String ARG_TITLE = "title"; 
    private static final String ARG_MESSAGE = "message"; 

    Context context = null; 

    private String title; 
    private String message; 
    private boolean submitData = false; 

    private OkCancelDialogListener mListener; 

    public OkCancelDialogFragment() { 
    } 

    public static OkCancelDialogFragment newInstance(String title, String message) { 
     OkCancelDialogFragment fragment = new OkCancelDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_TITLE, title); 
     args.putString(ARG_MESSAGE, message); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      title = getArguments().getString(ARG_TITLE); 
      message = getArguments().getString(ARG_MESSAGE); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle saveIntsanceState){ 

     context = getActivity(); 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     View rootView = inflater.inflate(R.layout.fragment_submit_cancel_dialog, null, false); 
     final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle); 
     final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage); 

     titleView.setText(title); 
     messView.setText(message); 

     builder.setView(rootView) 
       .setPositiveButton(R.string.button_Ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         submitData = true; 
         //The onDetach will call the Listener! Just in case the user taps the back button 
        } 
       }) 
       .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         submitData = false; 
        } 
       }); 
     return builder.create(); 
    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      if(mListener == null) mListener = (OkCancelDialogListener) context; 
     } 
     catch (Exception ex){ 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener.onFinishOkCancelDialog(submitData); 
     mListener = null; 
    } 


    public interface OkCancelDialogListener { 
     void onFinishOkCancelDialog(boolean submit); 
    } 

}