Android 使用 font-awesome 字体作为菜单项中的图标
Android use font-awesome font as icon in menu item
我只是在学习 android 很抱歉我对这个主题缺乏了解。我主要是一名 Web 开发人员,我们一直使用 Font Awesome,所以我试图让它与 Android 一起工作。我第一次发现我可以使用很棒的字体 here. With some deeper searching I found out how to put a font awesome icon in the action menu bar in the title here。我错过了在有空间时通过设置标题而不是图标和标题来拥有备份文本的能力。
我想知道我是否可以将图标设置为 awesome 字体而不是标题,并且标题只有正常的描述性文本。当我在 menu-item 上执行 setIcon 方法时,它需要一个可绘制对象或可绘制资源 ID。我可以将它转换为可绘制对象吗?保持图标的字体很棒并同时拥有图标和标题的最佳方法是什么?感谢任何帮助或指导。
所以我的主程序中有以下代码 activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.redbird_webkit_onwht);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
setFontAwesomeMenuItem(menu, R.string.icon_calendar, R.id.action_calendar);
setFontAwesomeMenuItem(menu, R.string.icon_search, R.id.action_search);
setFontAwesomeMenuItem(menu, R.string.icon_plus, R.id.action_new_post);
// menu.add(0, MENU_ITEM_LOGOUT, 102, R.string.logout);
return true;
}
private void setFontAwesomeMenuItem (Menu menu, int rIdString, int rIdIdOfElement) {
SpannableString s = new SpannableString(getString(rIdString));
s.setSpan(new TypefaceSpan(this, "fontawesome-webfont.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
menu.findItem(rIdIdOfElement).setTitle(s);
}
}
我从 here 得到了以下 TypefaceSpan class:
/**
* Style a {@link Spannable} with a custom {@link Typeface}.
*
* @author Tristan Waddington
* copied from
*/
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
回答晚了,但对于未来的访问者来说,有一个很棒的库可以在 android 中使用很棒的字体,称为 iconify,由 JoanZapata. You can find it here
编写
库正确初始化后,您可以使用 ActionBar 菜单项执行类似的操作:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="@+id/item_menu
showAsAction="always"
android:title="@string/title"/>
</menu>
从 java 代码你只需要做这样的事情:
menu.findItem(R.id.item_menu).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_share)
.colorRes(R.color.ab_icon)
.actionBarSize());
希望这对某人有所帮助:) 如有任何问题,请询问:)
请检查:
TextDrawable faIcon = new TextDrawable(this);
faIcon.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
faIcon.setTextAlign(Layout.Alignment.ALIGN_CENTER);
faIcon.setTypeface(FontAwesomeManager.getTypeface(this, FontAwesomeManager.FONTAWESOME));
faIcon.setText(getResources().getText(R.string.fa_android));
Menu menu = (Menu) findViewById(R.id.menu);
MenuItem menuItem = menu.findItem(R.id.menu_item);
menuItem.setIcon(faIcon);
您可以从 here
获取 TextDrawable class
我只是在学习 android 很抱歉我对这个主题缺乏了解。我主要是一名 Web 开发人员,我们一直使用 Font Awesome,所以我试图让它与 Android 一起工作。我第一次发现我可以使用很棒的字体 here. With some deeper searching I found out how to put a font awesome icon in the action menu bar in the title here。我错过了在有空间时通过设置标题而不是图标和标题来拥有备份文本的能力。
我想知道我是否可以将图标设置为 awesome 字体而不是标题,并且标题只有正常的描述性文本。当我在 menu-item 上执行 setIcon 方法时,它需要一个可绘制对象或可绘制资源 ID。我可以将它转换为可绘制对象吗?保持图标的字体很棒并同时拥有图标和标题的最佳方法是什么?感谢任何帮助或指导。
所以我的主程序中有以下代码 activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.redbird_webkit_onwht);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
setFontAwesomeMenuItem(menu, R.string.icon_calendar, R.id.action_calendar);
setFontAwesomeMenuItem(menu, R.string.icon_search, R.id.action_search);
setFontAwesomeMenuItem(menu, R.string.icon_plus, R.id.action_new_post);
// menu.add(0, MENU_ITEM_LOGOUT, 102, R.string.logout);
return true;
}
private void setFontAwesomeMenuItem (Menu menu, int rIdString, int rIdIdOfElement) {
SpannableString s = new SpannableString(getString(rIdString));
s.setSpan(new TypefaceSpan(this, "fontawesome-webfont.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
menu.findItem(rIdIdOfElement).setTitle(s);
}
}
我从 here 得到了以下 TypefaceSpan class:
/**
* Style a {@link Spannable} with a custom {@link Typeface}.
*
* @author Tristan Waddington
* copied from
*/
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
回答晚了,但对于未来的访问者来说,有一个很棒的库可以在 android 中使用很棒的字体,称为 iconify,由 JoanZapata. You can find it here
编写库正确初始化后,您可以使用 ActionBar 菜单项执行类似的操作:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="@+id/item_menu
showAsAction="always"
android:title="@string/title"/>
</menu>
从 java 代码你只需要做这样的事情:
menu.findItem(R.id.item_menu).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_share)
.colorRes(R.color.ab_icon)
.actionBarSize());
希望这对某人有所帮助:) 如有任何问题,请询问:)
请检查:
TextDrawable faIcon = new TextDrawable(this);
faIcon.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
faIcon.setTextAlign(Layout.Alignment.ALIGN_CENTER);
faIcon.setTypeface(FontAwesomeManager.getTypeface(this, FontAwesomeManager.FONTAWESOME));
faIcon.setText(getResources().getText(R.string.fa_android));
Menu menu = (Menu) findViewById(R.id.menu);
MenuItem menuItem = menu.findItem(R.id.menu_item);
menuItem.setIcon(faIcon);
您可以从 here
获取 TextDrawable class