簡體   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