我需要自定义字体和 TextView 方面的帮助

I need help in the custom Font and TextView

我是 android 的新手,正在开发一款应用程序,该应用程序可为用户提供有关 Google 的所有字体的信息。 为此,我需要制作一个带有 TextView Something like this

的应用

点击 TextView 后,字体会随着文字变化。

我正在考虑使用 onclicklistener

有两种存档方法

第一种方式

public class FontCache {

    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    public static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);

        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), fontname);
            } catch (Exception e) {
                return null;
            }

            fontCache.put(fontname, typeface);
        }

        return typeface;
    }
}

这会缓存字体,同时最大限度地减少对资产的访问次数。现在,因为我们有一个方法来访问我们的自定义字体,让我们实现一个 class,它扩展了 TextView。

扩展 TextView 接下来,我们将创建一个新的 Java class,它扩展了 TextView。这允许我们在所有 XML 视图中使用 class。它继承了常规 TextView 的所有功能和属性;但添加了我们的自定义字体。

我们再次查看 eat foody 项目的源代码。该代码可能看起来很复杂,但很简单:

public class EatFoodyTextView extends TextView {

    public EatFoodyTextView(Context context) {
        super(context);

        applyCustomFont(context);
    }

    public EatFoodyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        applyCustomFont(context);
    }

    public EatFoodyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
        setTypeface(customFont);
    }
}

前三个方法只是构造函数,我们重写它们以调用单个方法 applyCustomFont()。该方法是拼图的重要部分。它只是从我们的 FontCache class 中获取(希望缓存的)字体。最后,我们必须用字体调用 setTypeface(),我们就快完成了。如果您想知道,我们可以直接调用 setTypeface()(而不是在 TextView 对象上),因为我们正在扩展 TextView class.

使用 Class 您可能想知道,做这么多准备工作是否值得。在本节中,您将看到确实如此。因为您剩下要做的就是在 XML 视图中使用 class,它会自动具有您的自定义字体。无需 Java 代码!

<RelativeLayout  
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.futurestudio.foody.views.EatFoodyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/eat_foody_green_dark"
        android:textSize="20sp"
        android:text="Future Studio Blog"
        android:layout_marginBottom="24dp"/>

</RelativeLayout>  

如您所见,您可以继续使用 TextView 的所有细节(例如 textSize、textColor)。现在,只需用我们刚刚创建的 class 替换所有元素,例如,您在所有地方都应用了自定义字体! (参考:https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview

第二种方式 跟随 Google 指南支持 API 26 (Android 8) https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

在 textview 之间进行更改以更改字体

<RelativeLayout  
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/eat_foody_green_dark"
        android:textSize="20sp"
        android:text="Future Studio Blog"
        android:layout_marginBottom="24dp"/>
    <com.futurestudio.foody.views.EatFoodyTextView
        android:id="@+id/textview_custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/eat_foody_green_dark"
        android:textSize="20sp"
        android:text="Future Studio Blog"
        android:visibility="gone"
        android:layout_marginBottom="24dp"/>

</RelativeLayout> 

关注 android:visibility="gone" 在 Activity 中,您使用此代码在 2 个 TextViews

之间切换
    final TextView normalTextView = findViewById(R.id.textview_normal);
    final TextView customTextView = findViewById(R.id.textview_custom);

    normalTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            normalTextView.setVisibility(View.GONE);
            customTextView.setVisibility(View.VISIBLE);
        }
    });

    customTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            normalTextView.setVisibility(View.VISIBLE);
            customTextView.setVisibility(View.GONE);
        }
    });

您可以使用 android 属性通过 TextView、EditText、Button 等实现您自己的自定义字体。

如何

-这里有一些使用步骤:

1.Create属性文件(res->values->attrs.xml)

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="TextElement">
        <attr name="font" format="string"/>
        <attr name="underline" format="boolean"/>
    </declare-styleable>
</resources>

2.Create 自定义 TextView class(java 文件夹中的任意位置) 3. 在布局文件中使用属性 4. 只是 运行 你的代码。

这是你的问题的完整例子,你可以通过这个例子:

Full Demonstration

您可以将 "your_font.ttf" 文件放在资产文件夹中,然后使用

导入它
Typeface custom_font_1 = Typeface.createFromAsset(getAssets(),  "your_font.ttf");

然后用这个

将它分配给你的 showCaseTextView
   showCaseTextView.setTypeFace(custom_font_1);

然后在 showCaseTextViewonClickListener 中更改 specifiedTextView 字体,这样做

   specifiedTextView.setTypeFace(custom_font_1);

对其他字体重复此操作。