简体   繁体   中英

How can I add 'FontAwesome' icon on Android Action Toolbar Menu?

Is it possible to set an Icon in ActionBar "Text" using FontAwesome?

I have tried this way...

My menu item like that..

   <item
     android:id="@+id/action_chat"
     android:orderInCategory="100"
     android:title="@string/action_chat"
     app:showAsAction="always"
     android:actionLayout="@layout/chat_menu_icon"/>

My 'chat_menu_icon.xml'

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

   <com.bitmakers.techmonster.textview.FontAusomeTextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:autoLink="web"
         android:id="@+id/test"
         android:gravity="center|bottom"
         android:text="@string/action_chat"
         android:textSize="24sp"
         android:textStyle="bold" />
   </LinearLayout>

My Custom Font Class

    package com.bitmakers.techmonster.textview;

    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;

    public class FontAusomeTextView extends TextView {
         public static Typeface m_typeFace = null;

         public FontAusomeTextView(Context context) {
         super(context);
         // TODO Auto-generated constructor stub
         if (isInEditMode()) {
              return;
         }
         loadTypeFace(context);
     }

     public FontAusomeTextView(Context context, AttributeSet attrs) {
         super(context, attrs);
         if (isInEditMode()) {
              return;
          }
          loadTypeFace(context);
      }

      public FontAusomeTextView(Context context, AttributeSet attrs, int   defStyle) {
           super(context, attrs, defStyle);
           if (isInEditMode()) {
           return;
           }
           loadTypeFace(context);
       }

       private void loadTypeFace(Context context) {
           if (m_typeFace == null)
                m_typeFace = Typeface.createFromAsset(context.getAssets(),
                "fonts/fontawesome-webfont.ttf");
                this.setTypeface(m_typeFace);
       }
    }

It is working when i using it in normal textview

You can use a nice library called android-iconify to achieve your goal.

Here is how you implement it on Toolbar/Actionbar items:

your_menu_layout.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android"

   <item
       android:id="@+id/item_settings"
       showAsAction="ifRoom"
       android:title="@string/title"/>

</menu>

Inside your onCreateOptionsMenu method:

menu.findItem(R.id.item_settings).setIcon(
new IconDrawable(this, FontAwesomeIcons.fa_cog)
    .colorRes(R.color.your_color)
    .actionBarSize());

And you are good to go :)

Use Custom ActionBar

create your own layout and include that in xml using

<include.../> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM