2017-01-30 13 views
0

Ich versuche, font-awesome Symbole verwenden und ein Problem haben. Es funktionierte von MainActivity, aber wenn ich Fragment benutze, werden die Icons nicht angezeigt.(Android) font-awesome Symbole nicht angezeigt

Das ist mein HomeFragment

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View home = inflater.inflate(R.layout.fragment_home, container, false); 
    View feed_row = inflater.inflate(R.layout.feed_row, container, false); 

    like_button = (Button) feed_row.findViewById(R.id.like_button); 
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/fontawesome-webfont.ttf"); 
    like_button.setTypeface(tf); 

    mHomeFeed = (RecyclerView) home.findViewById(R.id.home_feed); 
    mLayoutManager = new LinearLayoutManager(this.getActivity()); 
    mHomeFeed.setHasFixedSize(true); 
    mHomeFeed.setLayoutManager(mLayoutManager); 

Dies ist feed_row.xml:

<Button 
    android:id="@+id/like_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/icon_heart" 
    android:background="@color/noBackground"/> 

strings.xml

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

Was soll ich jetzt tun? Es hatte keinen Fehler, aber Symbole sahen aus wie folgt:

this

Antwort

0

Statt mit:

Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), 
             "fonts/fontawesome-webfont.ttf"); 

die Speicherprobleme hat, weil ständig eine neue Schriftart für jedes Symbol (wie in dem answer Kommentar) zu schaffen, Sie können Calligraphy verwenden. Wo, nachdem Sie Ihre Anwendung mit der Bibliothek konfiguriert haben, müssen Sie nur hinzufügen fontPath Attribut:

So haben Sie einen mehr und flexibleren Code, in dem die Schriftart nur in der XML ist:

<Button 
    android:id="@+id/like_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/icon_heart" 
    android:background="@color/noBackground" 
    fontPath="fonts/fontawesome-webfont.ttf"/> 
0

Sie können dies tun, indem Sie eine Klasse zu machen und erweitert sie von Button Klasse wie dieses

public class AwesomeFontButton extends Button { 

    public AvenirBookEditText(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public AvenirBookEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public AvenirBookEditText(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     if (!isInEditMode()) { 
      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
        "fontawesome-webfont.ttf"); //add your font path here 
      setTypeface(tf); 
     } 
    } 

} 

und es in xML wie folgt verwenden:

<YourFilePath.AwesomeFontButton 
    android:id="@+id/like_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/icon_heart" 
    android:background="@color/noBackground"/> 

Verwendung es in xml, wo immer Sie

brauchen
Verwandte Themen