このコードはどこに配置すればよいですか
Button txt = (Button) findViewById(R.id.button1);
Typeface font = Typeface.createFromAsset(getAssets(), "customfont.ttf");
txt.setTypeface(font);
ボタンに表示されるテキストのフォントを変更できますか? ボタンのフォントを変更したいだけです。アラート ダイアログ ボックスのフォントを変更しようとしていません。ボタンのフォントを変更するコードをどこに置くべきかわかりません。アセットにある「fonts」フォルダー内にカスタムフォントを既に配置しています。
ボタンの xml 部分は次のとおりです。
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="alertBtn"
android:text="Click for message"
android:textSize="22sp"
/>
これが私のプロジェクトのコードです:
package com.test.hellothere;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloThereActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View v){}
public void alertBtn(View v){
new AlertDialog.Builder(this)
.setTitle("Hello")
.setMessage("Hello There!")
.setNeutralButton("Go Back", null)
.show();
}
}