0

私は Android アプリを作成していますが、ユーザーがアプリのフォント フェイス (フォント スタイル) を変更できるオプションを追加したいと考えています。フォント スタイルの変更ボタンが必要です。ユーザーがそれをクリックすると、いくつかのフォント スタイルのリストと、そのリストから選択したフォント スタイルが表示され、そのフォント スタイルに応じてアプリ テキスト全体が変更されます。皆さんの助けが必要です。簡単な方法ですべての手順を実行できますか。前もって感謝します。

4

6 に答える 6

1

私はこのコードでこれを達成しました:

   //Put font in asset/fonts folder

   String fontPath = "fonts/FONTNAME.TTF";

   // Loading Font Face
   Typeface typeface = Typeface.createFromAsset(getAssets(), fontPath);
    textview.setTypeface(typeface ); //setting font to particular view

それがあなたに役立つことを願っています。

于 2013-09-18T07:29:12.917 に答える
0
public class CustomFont extends Activity
{
ArrayList<String> fontsArrayList;
ListView font_list;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_fonts); // Layout containing a listView with id fontListView

    fontsArrayList=new ArrayList<String>();
    addDataToArrayList();

    font_list=(ListView)findViewById(R.id.fontListView);
    font_list.setAdapter(new FontListAdapter());
    font_list.setOnItemClickListener(FontSelected);
}

private void addDataToArrayList()
{
    fontsArrayList.add("Sans Serif");
    fontsArrayList.add("Droid Mono Space");
    fontsArrayList.add("Serif");
    fontsArrayList.add("Acid Structure");
            fontsArrayList.add("Ananda Neptouch");
//Other Fonts you wish to add
}

OnItemClickListener FontSelected=new OnItemClickListener()
{

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) 
{
    Typeface typeFace = SetTextAppearance.getFont(getAssets(), fontsArrayList.get(position));

  // You can get the font name from arraylist
     /* String fontName = fontsArrayList.get(position); */

// You can use the typeface for changing the font where you wish like
    /* textView.setTypeface(typeFace); */

}
};


// Adapter for ListView
public class FontListAdapter extends BaseAdapter
{


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return fontsArrayList.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View list=convertView;
    if(convertView==null)
    {
        LayoutInflater inflator=getLayoutInflater();
        list=inflator.inflate(R.layout.list_item, null); // Another Layout to be inflated as row for ListView. I have added a single textView in the layout
    }
    else
        list=(View)convertView;
    TextView textView=(TextView)list.findViewById(R.id.FontNameText);
    textView.setText(fontsArrayList.get(position));
    Typeface typeFace = SetTextAppearance.getFont(getAssets(), fontsArrayList.get(position));
    textView.setTypeface(typeFace);
    return list;
}

}

}

public class SetTextAppearance 
{
static AssetManager asm;

public static Typeface getFont(AssetManager mgr ,String fontName)
{
    Typeface typeFace = null;
    if (fontName.equals("Sans Serif")) {
        typeFace = Typeface.SANS_SERIF;
    }
    if (fontName.equals("Droid Mono Space")) {
        typeFace = Typeface.MONOSPACE;
    }
    if (fontName.equals("Serif")) {
        typeFace = Typeface.SERIF;
    }
    if (fontName.equals("Acid Structure")) {
        typeFace = Typeface.createFromAsset(mgr,"fonts/"+"acidstructure.ttf");
    }
    if (fontName.equals("Ananda Neptouch")) {
        typeFace = Typeface.createFromAsset(mgr,"fonts/"+"ananda_neptouch.ttf");
    }
}
}

assets/fonts にフォント ファイル (.ttf) がインポートされていることを確認してください。

于 2013-09-18T07:39:22.260 に答える
0

私は以下のようにそれをしました、

最初に、拡張機能付きのフォントをいくつかttfアセット フォルダーに保存します。

次に、作業アクティビティで、フォントをアセットから変数に取得します。

次に、それらを ListView またはスピナー、またはアプリケーションで必要なものに設定します。

Typeface オブジェクトを作成します。

その後、選択したリスト項目の位置を取得し、書体に設定します。

この書体をアプリケーションのテキストに設定し、

于 2013-09-18T07:26:47.937 に答える
0

最初に、すべてのフォントが適用された listView を作成するには、getView()メソッドで適切な TextView を返すAdapter を作成する必要があります。

フォントをeveryviewに適用するには、それが私がしたことです:

  • .ttf必要なファイルをforlderに/assets保存します
  • 次のようにメモリにロードします。

    Typeface font = Typeface.createFromAsset(c.getAssets(), "font.ttf");

  • getView (int position, View convertView, ViewGroup parent)メソッドで、位置に応じてフォントが適用された TextView を返すアダプターを作成します。

  • onItemClick すべてのTextView (または Typeface をサポートするすべてのビュー) に適切なフォントを再帰的に適用します。

    public void setTypefaceRecursive( View view, Typeface typeface ) {
        if( typeface == null )
            return;
    
        if( view instanceof ViewGroup ) {
            //set the typeface to all the sub views
            ViewGroup parent = (ViewGroup)view;
            for( int i = 0; i < parent.getChildCount(); i++ )
                setTypefaceRecursive( parent.getChildAt(i), typeface );
        } else if( view instanceof TextView ) {
            ((TextView) view).setTypeface(typeface);
        }
    }
    
  • Typefaceオブジェクトをグローバルな場所に保存し、インフレート/インスタンス化するすべてのビュー ツリーに適用します。

私がしなかったことは、リフレクションを使用して、 setTypeface(Typeface typface)メソッドを持つ可能性のあるすべてのビューにTypefaceを設定できることです。

    try {
        view.getClass().getMethod("setTypeface", Typeface.class ).invoke(view, typeface);
    }catch( NoSuchMethodException e ) {}
于 2013-09-18T07:41:55.007 に答える
0

これは、現在のアプリにフォントを適用するのに役立つ場合があります

http://www.androidguys.com/2008/08/18/fun-with-fonts/ にアクセスしてください。

于 2013-09-18T07:23:32.773 に答える