更改字体在AlertDialog [英] Changing font in AlertDialog

查看:105
本文介绍了更改字体在AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人提出一个方法来改变字体的动态创建AlertDialog(在标题和正文)?我试着用不同的方式加载,但没有一次成功。在code是:

 公共无效的onClick(视图v){

    新AlertDialog.Builder(C)
    .setTitle(data.eqselect)
    //  。组
    .setIcon(R.drawable.icon)
    .setMessage(Threads.myData [0])
    .setNegativeButton(关闭,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            Log.d(AlertDialog,负);
        }
    })
    。显示();
}
 

解决方案

而不是设置alertdialog的文字,您应该设置一个自定义视图形成你的布局。你这样做之前,修改视图的字体。

 的TextView为myContent =(TextView中)findViewById(R.id.yourid);
         mycontent.setTypeface(Typeface.createFromAsset(getAssets(),font.ttf)
 

和设置您的警告对话框的看法,电话 .setView(为myContent)而不是 setMessage() 尽管这不会改变你的标题是据我所知。

更新 我看到你是无法得到我在说什么,所以这里有一个完整的示例

  TextView的内容=新的TextView(本);
         content.setText(上的另一种字体);
         content.setTypeface(Typeface.SANS_SERIF);

//使用第一个例子,如果你使用的是生成的XML视图

     AlertDialog.Builder myalert =新AlertDialog.Builder(本);
         myalert.setTitle(你的标题);
         myalert.setView(内容);
         myalert.setNeutralButton(关闭对话框,NULL);
         myalert.setCancelable(真正的);
         myalert.show();
 

这是使用我们刚刚创建一个TextView,它不会有利润或此类。如果您使用的是容易产生一个形成了布局的文件,你可以自定义所有这些设置,但你可以做到这一点对这个例子在code。

当然,替换为你想要的字体.. 从XML样本TextView的可能是:

 <的TextView
        机器人:ID =@ + ID / yourid
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=8DP
        机器人:文本=您的内容/>
 

Could someone suggest a way to change font in a dynamically created AlertDialog (in title and body)? I tried in loads of ways, but none of them worked. The code is:

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();
}

解决方案

Instead of setting the text of the alertdialog, you should set a custom view form your layouts. And before you do so, modify your view's font.

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

And to set the view of your alert dialog, call .setView(mycontent) instead of setMessage() Although this doesn't change your title as far as I know.

Update I'm seeing you're unable to get what I'm saying, so here's a full example

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();

This is using a TextView that we've just created, and it won't have margins or such. If you use a readily created one form your layout files, you'd be able to customize all those settings, although you can do that for this example to in code.

Of course, replace the font with the one you want.. A sample TextView from xml could be:

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

这篇关于更改字体在AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆