繁体   English   中英

提示对话框如何更改字体?

[英]alert Dialog how can I change the font?

我需要将字体更改为alertDialog(存在Assets文件夹中的自定义字体),在对话框中有按钮(正号和负号)以及要检查的项目,如何将字体更改为对话框中的所有元素? 我正在用代码创建对话框,它不是带有xml的自定义对话框;

谢谢!

  1. 您应该使用dialogfragment实现此目的,创建要显示的设计
 public class ApproveDialog extends DialogFragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.custom_dialog, container, false); TextView styleText=(TextView)v.findViewById(R.id.styleText); Typdface tf=Typeface.createFromAsset(getResources().getAssets(),"abc.ttf"); styleText.setTypeface(tf); return v; } } 
  1. 调用片段使用
 FragmentTransaction ft=getActivity().getSupportFragmentManager().beginTransaction(); Fragment prev = getActivity().getSupportFragmentManager().findFragmentByTag("dialog1"); if (prev != null) ft.remove(prev); ft.addToBackStack(null); // Create and show the dialog. ApproveDialog newFragment = new ApproveDialog(); newFragment.show(ft, "dialog"); 
  1. custom_dialog.xml

<TextView id="@+id/styleText">

要更改您的Dialog 系统,可以使用以下代码。 您可以使用警报生成器来构建警报。 然后,您从此警报中获取TextView和EditView,然后设置警报的字体。

    AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello").setTitle("Title Alert").show();
    TextView textView = (TextView) dialog.findViewById(android.R.id.message);
    Typeface face= Typeface.createFromAsset(getAssets(), "segoe.ttf");
    int titleId = getResources().getIdentifier("alertTitle", "id", "android"); if (titleId > 0) {
        TextView dialogTitle = (TextView) dialog.findViewById(titleId);
        if (dialogTitle != null) {
            dialogTitle.setTypeface(face);
        }
    }
    textView.setTypeface(face);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM