-1

このコードはどこに配置すればよいですか

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

    }
4

2 に答える 2

1

onCreate次のようにActivityの中にコードを入れてくださいsetContentView

public class HelloThereActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
 Button txt;
 Typeface font;
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       txt = (Button) findViewById(R.id.button1);  

       // call here for setting font 
       setfonttoView(txt);

       //.....same for other buttons
    }
  public void setfonttoView(Button button){
       font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
       button.setTypeface(font);
    }

  //your code here...
于 2013-01-06T19:58:33.413 に答える
0
Button txt = (Button) findViewById(R.id.button1);  
Typeface font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
txt.setTypeface(font);

内部onCreate()

于 2013-01-06T19:58:41.087 に答える