5

誰かが動的に作成された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();
}
4

1 に答える 1

11

アラート ダイアログのテキストを設定する代わりに、レイアウトからカスタム ビューを設定する必要があります。その前に、ビューのフォントを変更してください。

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" />
于 2012-05-10T16:42:07.603 に答える