我创建了一个自定义 AlertDialog Builder ,我需要在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);
}
}
事实上,TextView是空的。如何定义TextView?我是初学者,请帮我用create custom alertdialog更改alertdialog的字体。
答案 0 :(得分:0)
我经常使用自定义对话框,所以我使用DialogFragment
。请注意,此对话框具有“确定”和“取消”按钮。如果您不需要,可以删除按钮。
您需要为自定义DialogFragment
“fragment_submit_cancel_dialog”创建XML布局。创建自己的设计的能力为您的对话框提供了极大的灵活性。
在您调用DialogFragment
的活动中,您需要添加以下内容:
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");
}
现在自定义对话框片段的代码:
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);
}
}