【问题标题】:I need help in the custom Font and TextView我需要自定义 Font 和 TextView 的帮助
【发布时间】:2018-09-24 03:21:51
【问题描述】:

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

的应用程序

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

我正在考虑使用 onclicklistener

【问题讨论】:

  • 提问前你在谷歌搜索过吗?
  • @FaizMir 是的,我只做我从谷歌得到的自定义字体主题,只有自定义字体不是我的问题

标签: android fonts textview


【解决方案1】:

你可以把"your_font.ttf"文件放在asset文件夹里,然后用

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

然后将其分配给您的showCaseTextView

   showCaseTextView.setTypeFace(custom_font_1);

然后在您的onClickListenershowCaseTextView 中更改您的specifiedTextView 字体这样做

   specifiedTextView.setTypeFace(custom_font_1);

并为其他字体重复此操作。

【讨论】:

  • 感谢这对我帮助很大,
  • 很高兴听到那个克里希纳
  • 我需要一点帮助如何联系你请告诉我
  • 你可以联系我的电报或whats app账号+98-935-589-0011
  • 抱歉,我没有使用您的电子邮件或任何社交帐户
【解决方案2】:

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

如何

-以下是一些使用步骤:

1.创建属性文件(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.创建自定义 TextView 类(在 java 文件夹中的任何位置) 3. 在布局文件中使用属性 4. 运行您的代码。

这是您问题的完整示例,您可以查看此示例:

Full Demonstration

【讨论】:

  • 我可以将它与 OnClick 一起使用它会有所帮助
  • 此代码如果用于自定义视图我想在点击时更改字体
【解决方案3】:

有两种存档方式

第一种方式

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

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

扩展 TextView 接下来,我们将创建一个扩展 TextView 的新 Java 类。这允许我们在所有 XML 视图中使用该类。它继承了常规 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 类中获取(希望缓存的)字体。最后,我们必须使用字体调用 setTypeface(),我们几乎完成了。如果您想知道,我们可以直接调用 setTypeface()(而不是在 TextView 对象上),因为我们正在扩展 TextView 类。

使用类 您可能想知道,如此多的准备是否值得付出努力。在本节中,您将看到确实如此。因为您剩下要做的就是在 XML 视图中使用该类,它会自动拥有您的自定义字体。不需要 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)。现在,只需用我们刚刚创建的类替换所有元素,例如,您在任何地方都应用了自定义字体! (参考:https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview

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

在 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 个 TextView 之间切换

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

【讨论】:

  • 点击 TixtView 字体没有改变
  • 你能告诉我如何使用 OnClickListener 来做到这一点
  • 我建议您使用 2 个文本视图(1 个普通字体,1 个自定义字体)。 onClickListener 仅用于切换可见性以在 2 个文本视图之间显示
  • 有什么帮助我该怎么做
  • 我为解决方案添加更多代码在普通字体和自定义字体之间切换 2 TextView
猜你喜欢
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
相关资源
最近更新 更多