簡體   English   中英

(Android)字體超贊的圖標未顯示

[英](Android) font-awesome icons not showing

我正在嘗試使用超棒的字體圖標並遇到一些問題。 它可以從MainActivity使用,但是當我從Fragment使用它時,它不會顯示圖標。

這是我的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);

這是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>

我現在應該怎么辦? 它沒有錯誤,但是圖標如下所示:

這個

而是使用:

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

由於存在不斷為每個圖標創建新字體(如答案注釋中所述)的問題,因此可以使用Calligraphy 在使用庫配置應用程序之后,只需添加fontPath屬性:

因此,您有一個更靈活的代碼,其中的字體僅在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"
   fontPath="fonts/fontawesome-webfont.ttf"/>

您可以通過創建一個類並從Button類擴展它來完成此操作,如下所示

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);
        }
    }

}

並像這樣在xml中使用它:

<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"/>

隨時隨地在xml中使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM