mikepenz / Android-Iconics

Android-Iconics - Use any icon font, or vector (.svg) as drawable in your application.
https://mikepenz.dev
Apache License 2.0
5.16k stars 619 forks source link

Using custom fonts, icons can be displayed in Java code, but not in XML #607

Closed zhuzhuxiang closed 2 years ago

zhuzhuxiang commented 2 years ago
      Drawable drawable_custom = new IconicsDrawable(this)
            .icon(Cniao5Font.Icon.zhu__denglong)
            .color(Color.GREEN)
            .sizeDp(150);

    ((ImageView) this.findViewById(R.id.image_custom)).setImageDrawable(drawable_custom);   √

  <TextView
       android:layout_width="50dp"
       android:layout_height="50dp"
       android:text="自定义字体{zhu_denglong}" />   ×
mikepenz commented 2 years ago

@zhuzhuxiang could you please provide more information?

How does the specified font look like? did you define the 3 character key as zhu?

Do you either register the font when you set up Iconics or have an initializer like the provided fonts?

Initializer: https://github.com/mikepenz/Android-Iconics/blob/develop/meteocons-typeface-library/src/main/java/com/mikepenz/iconics/typeface/library/meteoconcs/Initializer.kt#L8-L17 https://github.com/mikepenz/Android-Iconics/blob/develop/meteocons-typeface-library/src/main/AndroidManifest.xml#L28-L30

zhuzhuxiang commented 2 years ago
      Drawable drawable_custom = new IconicsDrawable(this)
            .icon(Cniao5Font.Icon.zhu__denglong)
            .color(Color.GREEN)
            .sizeDp(150);

    ((ImageView) this.findViewById(R.id.image_custom)).setImageDrawable(drawable_custom);   √

  <TextView
       android:layout_width="50dp"
       android:layout_height="50dp"
       android:text="自定义字体{zhu_denglong}" />   ×
mikepenz commented 2 years ago

You duplicated the initial message. Please see my answer above :)

zhuzhuxiang commented 2 years ago

IMG_20211019_222211

   <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="{ion-help-buoy}" />

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="{zhu_denglong}" />

    <ImageView
        android:id="@+id/image_custom"
        android:layout_width="50dp"
        android:layout_height="50dp" />
 Drawable drawable = new IconicsDrawable(this)
                .icon(Ionicons.Icon.ion_android_add_circle)
                .color(Color.RED)
                .sizeDp(100);

        ((ImageView) this.findViewById(R.id.image_view)).setImageDrawable(drawable);

Drawable drawable_custom = new IconicsDrawable(this)
                .icon(Cniao5Font.Icon.zhu__denglong)
                .color(Color.GREEN)
                .sizeDp(100);

        ((ImageView) this.findViewById(R.id.image_custom)).setImageDrawable(drawable_custom);

The two imageviews and the first textview can be displayed. The second textview uses a custom font. I want to display icons in the form of {} like the first textview. I don't know what to do

zhu Defined. Ionicons class for custom font rewriting

   @Override
    public String getMappingPrefix() {    
        return "zhu";
    }     
    public enum Icon implements IIcon {
        zhu_gu('\ue60a'),
        zhu__chahu('\ue60b'),
        zhu__xianglu('\ue60c'),
        zhu__denglong('\ue60d'),
        zhu__fudao('\ue60e');
zhuzhuxiang commented 2 years ago
   //only required if you add a custom or generic font on your own
    Iconics.init(getApplicationContext());

    //register custom fonts like this (or also provide a font definition file)
    Iconics.registerFont(new Cniao5Font());

I'm registered
第一个TextView xml android:text="{ion-help-buoy}"

mikepenz commented 2 years ago

The Icon class enum should only have a single _ for the names.

See one off the icons provided from the library here: https://github.com/mikepenz/Android-Iconics/blob/develop/meteocons-typeface-library/src/main/java/com/mikepenz/iconics/typeface/library/meteoconcs/Meteoconcs.kt

I also see you use Java. Only the latest version of the library is fully supported which is targeted for Kotlin.

saeed74 commented 2 years ago

thanks for this topic! registering my custom font in Application class was the thing that I forgot:

Iconics.init(getApplicationContext()); Iconics.registerFont( MaterialDesignIcon.INSTANCE );