如何使用“中的图标和符号”;字体真棒“;论原生Android应用

如何使用“中的图标和符号”;字体真棒“;论原生Android应用,android,font-awesome,Android,Font Awesome,我正在尝试在我的应用程序上使用,我能够使用Typeface.createFromAsset()集成字体,但我也想使用此字体提供的图标,但到目前为止我还无法做到这一点 这种特殊字体在Unicode专用区域(PUA)内包含图标,用于媒体播放器控件、文件系统访问、箭头等 有人在Android上使用过包含图标和符号的字体吗?这有可能吗?试试IcoMoon: 选择你想要的图标 为每个图标指定字符 下载字体 比如说,您选择了播放图标,将字母“p”分配给它,并将文件icomoon.ttf下载到您的资产文件

我正在尝试在我的应用程序上使用,我能够使用
Typeface.createFromAsset()
集成字体,但我也想使用此字体提供的图标,但到目前为止我还无法做到这一点

这种特殊字体在Unicode专用区域(PUA)内包含图标,用于媒体播放器控件、文件系统访问、箭头等

有人在Android上使用过包含图标和符号的字体吗?这有可能吗?

试试IcoMoon:

  • 选择你想要的图标
  • 为每个图标指定字符
  • 下载字体
比如说,您选择了播放图标,将字母“p”分配给它,并将文件
icomoon.ttf
下载到您的资产文件夹中。以下是显示图标的方式:

xml:

我做了一个关于制作漂亮的Android应用程序的演讲,其中包括关于使用图标字体的解释,以及添加渐变以使图标更漂亮:

图标字体说明从幻灯片34开始:

字体在我的android应用程序中,真棒似乎很适合我。我做了以下工作:

  • 已将
    Fontsome webfont.ttf
    复制到我的资产文件夹中
  • 使用此页面找到我想要的图标的角色实体:
  • 在strings.xml中为每个图标创建一个条目。例如对于心脏来说:

    <string name="icon_heart">&#xf004;</string>
    

  • 我参加聚会有点晚了,但我编写了一个自定义视图,让您可以这样做。默认情况下,它设置为entypo,但您可以修改它以使用任何iconfont:在这里查看:github.com/MarsVard/IconView

    // 编辑库已过时,不再受支持。。。
    这里有一个新的

    可能太晚了,但我也有同样的需要,所以我发布了这个 正如Keith Corwin所说,这是一个android版本的xml字体


    希望它能帮助其他人。

    FontView库允许您在应用程序中使用普通/unicode字体字符作为图标/图形。它可以通过资源或网络位置加载字体

    该库的好处在于:

    1 - it takes care of remote resources for you
    2 - scales the font size in dynamically sized views
    3 - allows the font to easily be styled.
    

    例如:

    布局:

    <com.finalhack.fontview.FontView
            android:id="@+id/someActionIcon"
            android:layout_width="80dp"
            android:layout_height="80dp" />
    

    如果您只需要几个字体很棒的图标,还可以使用来生成普通像素图像。但是如果你经常添加新的图标/按钮,我会推荐.ttf版本,因为它更灵活

    有一些小而有用的:

    开始演示。

    您可以轻松地在布局中添加基于字体的图标:

    <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"
        />
    
    Java代码:

    Drawable icon = FontIconDrawable.inflate(getResources(), R.xml.ic_android);
    
    链接:


    我使用的字体库之一是:

    具体来说,

    文档很容易理解

    首先,在build.gradle中添加所需的依赖项:

    dependencies {
        compile 'com.beardedhen:androidbootstrap:1.2.3'
    }
    
    其次,您可以在XML中添加以下内容:

    <com.beardedhen.androidbootstrap.FontAwesomeText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fontawesometext:fa_icon="fa-github"
        android:layout_margin="10dp" 
        android:textSize="32sp"
    />
    

    如果有人想知道如何以编程方式添加它,你必须这样做

       button_info.setText(R.string.icon_heart);
        button_info.append(" Hallo"); //<<--- This is the tricky part
    
    button\u info.setText(R.string.icon\u heart);
    
    按钮信息附加(“哈罗”)// 如果希望在不向string.xml添加字符串的情况下使用编程setText

    请参见此处的十六进制代码:

    更换;至0xf066

     Typeface typeface = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
        textView.setTypeface(typeface);
        textView.setText(new String(new char[]{0xf006 }));
    

    如上所述是一个很好的例子,效果也很好:

    Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf" );
    
    Button button = (Button)findViewById( R.id.like );
    button.setTypeface(font);
    
    但是!>如果从xml设置按钮内的字符串:

    <string name="icon_heart">&#xf004;</string>
    button.setText(getString(R.string.icon_heart));
    

    我在C#(Xamarin)中创建了这个helper类,以编程方式设置text属性。这对我来说非常有效:

    internal static class FontAwesomeManager
    {
        private static readonly Typeface AwesomeFont = Typeface.CreateFromAsset(App.Application.Context.Assets, "FontAwesome.ttf");
    
        private static readonly Dictionary<FontAwesomeIcon, string> IconMap = new Dictionary<FontAwesomeIcon, string>
        {
            {FontAwesomeIcon.Bars, "\uf0c9"},
            {FontAwesomeIcon.Calendar, "\uf073"},
            {FontAwesomeIcon.Child, "\uf1ae"},
            {FontAwesomeIcon.Cog, "\uf013"},
            {FontAwesomeIcon.Eye, "\uf06e"},
            {FontAwesomeIcon.Filter, "\uf0b0"},
            {FontAwesomeIcon.Link, "\uf0c1"},
            {FontAwesomeIcon.ListOrderedList, "\uf0cb"},
            {FontAwesomeIcon.PencilSquareOutline, "\uf044"},
            {FontAwesomeIcon.Picture, "\uf03e"},
            {FontAwesomeIcon.PlayCircleOutline, "\uf01d"},
            {FontAwesomeIcon.SignOut, "\uf08b"},
            {FontAwesomeIcon.Sliders, "\uf1de"}
        };
    
        public static void Awesomify(this TextView view, FontAwesomeIcon icon)
        {
            var iconString = IconMap[icon];
    
            view.Text = iconString;
            view.SetTypeface(AwesomeFont, TypefaceStyle.Normal);
        }
    }
    
    enum FontAwesomeIcon
    {
        Bars,
        Calendar,
        Child,
        Cog,
        Eye,
        Filter,
        Link,
        ListOrderedList,
        PencilSquareOutline,
        Picture,
        PlayCircleOutline,
        SignOut,
        Sliders
    }
    
    内部静态类FontAwesomeManager
    {
    私有静态只读字体AwesomeFont=Typeface.CreateFromAsset(App.Application.Context.Assets,“FontAwesome.ttf”);
    专用静态只读词典IconMap=新词典
    {
    {FontAwesomeIcon.bar,“\uf0c9”},
    {FontAwesomeIcon.Calendar,“\uf073”},
    {FontAwesomeIcon.Child,“\uf1ae”},
    {FontAwesomeIcon.Cog,“\uf013”},
    {FontAwesomeIcon.Eye,“\uf06e”},
    {FontAwesomeIcon.Filter,“\uf0b0”},
    {FontAwesomeIcon.Link,“\uf0c1”},
    {FontAwesomeIcon.ListOrderedList,“\uf0cb”},
    {FontAwesomeIcon.PencilSquareOutline,“\uf044”},
    {FontAwesomeIcon.Picture,“\uf03e”},
    {FontAwesomeIcon.PlayCircleOutline,“\uf01d”},
    {FontAwesomeIcon.SignOut,“\uf08b”},
    {FontAwesomeIcon.Sliders,“\uf1de”}
    };
    公共静态void Awesomify(此文本视图,FontAwesomeIcon图标)
    {
    var iconString=IconMap[icon];
    view.Text=iconString;
    view.SetTypeface(AwesomeFont,TypefaceStyle.Normal);
    }
    }
    枚举FontAwesomeIcon
    {
    酒吧,
    日历,
    小孩
    齿轮
    眼睛,
    过滤器,
    链接
    ListOrderedList,
    铅笔轮廓,
    图片,
    游戏大纲,
    签出,
    滑块
    }
    

    我认为转换成Java应该很容易。希望它能帮助别人

    还有一个很好的解决方案,您可以直接在布局xml文件中使用,而不需要使用
    setTypeface

    是琼·萨帕塔的。您可以阅读Iconify v2中的新增内容。它包括9个不同的字体库,您只需向build.gradle文件添加依赖项即可使用

    在布局xml文件中,可以在以下小部件之间进行选择:

    com.joanzapata.iconify.widget.IconTextview
    com.joanzapata.iconify.widget.IconButton
    com.joanzapata.iconify.widget.IconToggleButton
    

    所有答案都很好,但我不想使用库,每个解决方案只有一行java代码,这使得我的
    活动和
    片段非常混乱。
    因此我重写了
    TextView
    类,如下所示:

    public class FontAwesomeTextView extends TextView {
    private static final String TAG = "TextViewFontAwesome";
    public FontAwesomeTextView(Context context) {
        super(context);
        init();
    }
    
    public FontAwesomeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    
    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }
    
    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fontawesome-webfont.ttf");
            setTypeface(tf);
        }
    }
    
    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }
    
        setTypeface(typeface);
        return true;
    }
    }
    
    您应该做的是将font
    ttf
    文件复制到
    assets
    文件夹中。并用于查找每个图标字符串


    希望这有帮助。

    首先创建资产文件夹并复制fontawesome图标(.ttf) 如何创建资产文件夹

    应用程序-->右键单击-->新建-->文件夹-->资产文件夹

    下一步下载如何下载.ttf文件? 下载解压缩并打开web字体后,单击下载按钮。最后选择真文本样式(ttf)粘贴资源文件夹

    如何在android中设计xml和java文件

        xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
    
       button_info.setText(R.string.icon_heart);
        button_info.append(" Hallo"); //<<--- This is the tricky part
    
     Typeface typeface = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
        textView.setTypeface(typeface);
        textView.setText(new String(new char[]{0xf006 }));
    
    Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf" );
    
    Button button = (Button)findViewById( R.id.like );
    button.setTypeface(font);
    
    <string name="icon_heart">&#xf004;</string>
    button.setText(getString(R.string.icon_heart));
    
    String iconHeart = "&#xf004;";
    String valHexStr = iconHeart.replace("&#x", "").replace(";", "");
    long valLong = Long.parseLong(valHexStr,16);
    button.setText((char) valLong + "");
    
    internal static class FontAwesomeManager
    {
        private static readonly Typeface AwesomeFont = Typeface.CreateFromAsset(App.Application.Context.Assets, "FontAwesome.ttf");
    
        private static readonly Dictionary<FontAwesomeIcon, string> IconMap = new Dictionary<FontAwesomeIcon, string>
        {
            {FontAwesomeIcon.Bars, "\uf0c9"},
            {FontAwesomeIcon.Calendar, "\uf073"},
            {FontAwesomeIcon.Child, "\uf1ae"},
            {FontAwesomeIcon.Cog, "\uf013"},
            {FontAwesomeIcon.Eye, "\uf06e"},
            {FontAwesomeIcon.Filter, "\uf0b0"},
            {FontAwesomeIcon.Link, "\uf0c1"},
            {FontAwesomeIcon.ListOrderedList, "\uf0cb"},
            {FontAwesomeIcon.PencilSquareOutline, "\uf044"},
            {FontAwesomeIcon.Picture, "\uf03e"},
            {FontAwesomeIcon.PlayCircleOutline, "\uf01d"},
            {FontAwesomeIcon.SignOut, "\uf08b"},
            {FontAwesomeIcon.Sliders, "\uf1de"}
        };
    
        public static void Awesomify(this TextView view, FontAwesomeIcon icon)
        {
            var iconString = IconMap[icon];
    
            view.Text = iconString;
            view.SetTypeface(AwesomeFont, TypefaceStyle.Normal);
        }
    }
    
    enum FontAwesomeIcon
    {
        Bars,
        Calendar,
        Child,
        Cog,
        Eye,
        Filter,
        Link,
        ListOrderedList,
        PencilSquareOutline,
        Picture,
        PlayCircleOutline,
        SignOut,
        Sliders
    }
    
    com.joanzapata.iconify.widget.IconTextview
    com.joanzapata.iconify.widget.IconButton
    com.joanzapata.iconify.widget.IconToggleButton
    
    public class FontAwesomeTextView extends TextView {
    private static final String TAG = "TextViewFontAwesome";
    public FontAwesomeTextView(Context context) {
        super(context);
        init();
    }
    
    public FontAwesomeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    
    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }
    
    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fontawesome-webfont.ttf");
            setTypeface(tf);
        }
    }
    
    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }
    
        setTypeface(typeface);
        return true;
    }
    }
    
    resources
        string name="calander_font" >&#xf073; <string
    resources
    
     <TextView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/calander_view"/>
    
    calander_tv = (TextView)findViewById(R.id.calander_view);
    
    Typeface typeface = Typeface.createFromAsset(getAssets(),"/fonts/fa-solid-900.ttf");
    calander_tv.setTypeface(typeface);
    calander_tv.setText(R.string.calander_font);