java - 更改 AlertDialog 中的字体

标签 java android android-alertdialog

有人可以建议一种在动态创建的 AlertDialog(标题和正文中)中更改字体的方法吗?我尝试了很多方法,但都没有用。代码是:

public void onClick(View v) {

    new AlertDialog.Builder( c )
    .setTitle( data.eqselect )
    //  .set 
    .setIcon(R.drawable.icon)
    .setMessage(Threads.myData[0] )
    .setNegativeButton( "Close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d( "AlertDialog", "Negative" );
        }
    } )
    .show();
}

最佳答案

您应该从布局中设置自定义 View ,而不是设置警报对话框的文本。在这样做之前,请修改 View 的字体。

TextView mycontent = (TextView) findViewById(R.id.yourid);
         mycontent.setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf")

要设置警报对话框的 View ,请调用 .setView(mycontent) 而不是 setMessage() 尽管据我所知这不会改变您的头衔。

更新 我发现你无法理解我在说什么,所以这里有一个完整的例子

TextView content = new TextView(this);
         content.setText("on another font");
         content.setTypeface(Typeface.SANS_SERIF);

//Use the first example, if your using a xml generated view

     AlertDialog.Builder myalert = new AlertDialog.Builder(this);
         myalert.setTitle("Your title");
         myalert.setView(content);
         myalert.setNeutralButton("Close dialog", null);
         myalert.setCancelable(true);
         myalert.show();

这是使用我们刚刚创建的 TextView,它没有边距等。如果您使用一个易于创建的布局文件,您将能够自定义所有这些设置,尽管您可以在代码中为此示例执行此操作。

当然,把字体换成你想要的.. 来自 xml 的示例 TextView 可以是:

<TextView
        android:id="@+id/yourid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="your content" />

关于java - 更改 AlertDialog 中的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10538482/

相关文章:

java - 尝试解析日期时显示错误的日期

java - 拼写检查框架可以用于代码提示吗?

java - Java中没有 "extends"关键字的继承

java - GWT ImageCell 到 CellTable

Android:从通知启动外部应用程序

android - 使用 android.support.v7.app.AlertDialog 自定义默认的 Theme.AppCompat.Dialog.Alert

android - 无法在 Eclipse 中调试 Android 应用程序空白线程

android - 混合和卫星 View 在 android google maps api 中不起作用

android - 使用带有警报对话框的最新 Material 概述按钮

android - Alertdialog 不起作用,它在 Android 中产生异常