【问题标题】:Changing font in AlertDialog在 AlertDialog 中更改字体
【发布时间】:2012-12-24 22:28:22
【问题描述】:

有人可以建议一种方法来更改动态创建的 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();
}

【问题讨论】:

    标签: java android android-alertdialog


    【解决方案1】:

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

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

    要设置警报对话框的视图,请调用 .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" />
    

    【讨论】:

    • 会以同样的方式弹出吗?
    • 当然,您只是告诉他使用自定义视图而不是默认视图。试一试
    • 但它不会作为不占用整个屏幕的警报对话框弹出,并且布局会
    • 要么你不理解答案,要么我不理解这个问题。阅读更新
    • 在哪里可以找到我需要制作的视图布局示例?因为我不明白它是如何工作的
    猜你喜欢
    • 2011-11-20
    • 2014-10-13
    • 2011-09-27
    • 1970-01-01
    • 2013-02-28
    • 2023-03-22
    • 2015-07-06
    • 2011-05-04
    • 1970-01-01
    相关资源
    最近更新 更多