1

私は、それぞれ異なるビューを持つ多くのクラスを持つこのアンドロイドコードを持っています。設定に移動し、選択したフォントに応じてフォントを変更できます。現在、プリインストールされている Android フォントのみが利用可能です。.ttf ファイルを追加してフォントのオプションとして提供できるように、コードを少し調整する方法はありますか。コードに大きな変更を加えたくありません。

4

2 に答える 2

3

書体を使用して、textview のテキストにカスタム フォントを設定できます。したがって、テキストビューにカスタムフォントが必要な場合はいつでも、以下を使用できます.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />
 </LinearLayout>

コード:

public class MainActivity extends Activity {

private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener( new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            text.setText("This is a custom toast");
            Typeface typeFace =  Typeface.createFromAsset(getAssets(),"fonts/kn.ttf");
            text.setTypeface(typeFace);

        }

    });

}
}

ここに画像の説明を入力

于 2013-04-13T18:49:51.230 に答える
1

いいえ、カスタム フォントで独自のビューを使用しない限り、できません。

于 2013-04-13T18:49:04.880 に答える