Jul*_*rez 152 android font-awesome
我正在尝试在我的应用程序上使用Font Awesome,我能够使用该字体集成Typeface.createFromAsset(),但我也想使用这种字体提供的图标,但到目前为止我还没能做到这一点.
此特定字体包含Unicode专用区(PUA)内的图标,用于媒体播放器控件,文件系统访问,箭头等内容.
有没有人在Android上使用包含图标和符号的字体,这有可能吗?
小智 305
在我的Android应用程序中,Font Awesome似乎对我很好.我做了以下事情:
fontawesome-webfont.ttf到我的assests文件夹中在strings.xml中为每个图标创建了一个条目.例如,一颗心:
<string name="icon_heart"></string>
Run Code Online (Sandbox Code Playgroud)在我的xml布局视图中引用了所述条目:
<Button
android:id="@+id/like"
style="?android:attr/buttonStyleSmall"
...
android:text="@string/icon_heart" />
Run Code Online (Sandbox Code Playgroud)在我的onCreate方法中加载字体并将其设置为适当的视图:
Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)chi*_*uki 28
试试IcoMoon:http://icomoon.io
比如说,您选择了播放图标,为其分配了字母"P",并将文件下载icomoon.ttf到资产文件夹中.这是你显示图标的方式:
XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:text="P" />
Run Code Online (Sandbox Code Playgroud)
Java的:
Typeface typeface = Typeface.createFromAsset(getAssets(), "icomoon.ttf");
textView.setTypeface(typeface);
Run Code Online (Sandbox Code Playgroud)
我已经谈过制作精美的Android应用程序,其中包括使用图标字体的说明,以及添加渐变以使图标更漂亮:http: //www.sqisland.com/talks/beautiful-android
图标字体说明从幻灯片34开始:http: //www.sqisland.com/talks/beautiful-android/#34
lil*_*tof 10
也许为时已晚,但我有同样的需求,所以我发布了这个https://github.com/liltof/font-awsome-for-android 这是一个Android准备好的xml版本的字体真棒可用就像Keith Corwin说的
希望它会帮助别人.
以上是很好的例子,效果很好:
Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf" );
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)
但!>如果从xml设置的按钮内的字符串,这将有效:
<string name="icon_heart"></string>
button.setText(getString(R.string.icon_heart));
Run Code Online (Sandbox Code Playgroud)
如果需要动态添加它可以使用:
String iconHeart = "";
String valHexStr = iconHeart.replace("&#x", "").replace(";", "");
long valLong = Long.parseLong(valHexStr,16);
button.setText((char) valLong + "");
Run Code Online (Sandbox Code Playgroud)
有一个小而有用的库是为此目的而设计的:
dependencies {
compile 'com.shamanland:fonticon:0.1.9'
}
Run Code Online (Sandbox Code Playgroud)
在Google Play上获取演示。

您可以轻松地在布局中添加基于字体的图标:
<com.shamanland.fonticon.FontIconView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ic_android"
android:textSize="@dimen/icon_size"
android:textColor="@color/icon_color"
/>
Run Code Online (Sandbox Code Playgroud)
您可以Drawable从 xml 中扩充 font-icon:
<?xml version="1.0" encoding="utf-8"?>
<font-icon
xmlns:android="http://schemas.android.com/apk/res-auto"
android:text="@string/ic_android"
android:textSize="@dimen/big_icon_size"
android:textColor="@color/green_170"
/>
Run Code Online (Sandbox Code Playgroud)
Java代码:
Drawable icon = FontIconDrawable.inflate(getResources(), R.xml.ic_android);
Run Code Online (Sandbox Code Playgroud)
链接:
| 归档时间: |
|
| 查看次数: |
109344 次 |
| 最近记录: |