安卓:Crouton库和自定义字体

4

我在我的应用程序中使用自定义字体,因此我想要一个适用于 Crouton 的自定义字体。我尝试使用 setTextAppearance 实现它,但它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<com.ecab.ui.custom.TextViewCustomFont
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.crouton"
    android:id="@+id/crouton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ban_confirmation"
    android:gravity="center"
    android:text="TEST"
    android:textColor="@android:color/white"
    custom:typeface="gothamBold" />

在样式类中:

INFOCUSTOM = new Builder().setDuration(3000).setTextAppearance(R.id.crouton).build();

我尝试使用我的字体替换setTypeface()来完成它,但失败了。

在Crouton类中:

private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
    text.setId(TEXT_ID);
    text.setText(this.text);
    text.setTypeface(MyFonts.getGothamBookBold(this.activity));
    Log.d(Constants.D_TAG, "chaneg the typeFace");
    text.setGravity(this.style.gravity);
    // set the text color if set
    if (this.style.textColorResourceId != 0) {
      text.setTextColor(resources.getColor(this.style.textColorResourceId));
    }

    // Set the text size. If the user has set a text size and text
    // appearance, the text size in the text appearance
    // will override this.
    if (this.style.textSize != 0) {
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.style.textSize);
    }

    // Setup the shadow if requested
    if (this.style.textShadowColorResId != 0) {
      initializeTextViewShadow(resources, text);
    }

    // Set the text appearance
    if (this.style.textAppearanceResId != 0) {
      text.setTextAppearance(this.activity, this.style.textAppearanceResId);
    }
    return text;
  }

我应该怎样才能使用自定义字体?

注:库版本 ==> 1.7


1
这个答案可能会有所帮助:http://stackoverflow.com/a/15223451/543711。 - iTurki
你尝试过创建者在这里建议的方法吗:http://stackoverflow.com/a/15223451/644669? - Zakaria
是的!我知道这个问题,但它不起作用... - mrroboaat
可能是使用自定义布局的crouton库的重复问题。 - Ben Weiss
@aatt 你是如何编辑Crouton.class文件的?它被锁定在build/intermediates/exploded-aar/de-key..../crouton/1.8.5/classes.jar中。 - ono
@ono库是开源的,因此您可以修改源代码并将其添加到您的项目中。 - mrroboaat
3个回答

2

好的,我找到问题了!

通过更改字体,第二个解决方案可以解决问题。我只是忘记删除

这段代码。

setTextAppearance(R.id.crouton)

在Style类中。因此,我的自定义样式如下:
INFOCUSTOM = new Builder().setDuration(3000).setBackgroundDrawable(R.drawable.ban_confirmation).setHeight(LayoutParams.WRAP_CONTENT)
          .build();

一个问题解决了,另一个问题又来了 :)!使用背景可绘制对象时,文本不能垂直居中。


1

您可以使用Style.Builder.setTextAppearance(...)创建一个自定义样式,该样式使用文本外观的resourceId。

这将引用您styles.xml中的引用,并在Crouton的内部TextView中使用它。

然后,您可以使用自定义Style调用Crouton.makeText或Crouton.showText。

来源


0

MyFonts.getGothamBookBold() 是什么样子?

然而,这应该可以工作:

  private TextView initializeTextView(final Resources resources) {
    TextView text = new TextView(this.activity);
    text.setId(TEXT_ID);
    text.setText(this.text);
    Typeface myTypeFace = Typeface.createFromAsset(this.activity.getAssets(), "gothamBold.ttf"); 
    text.setTypeface(myTypeFace);
    text.setGravity(this.style.gravity);

    // set the text color if set
    if (this.style.textColorResourceId != 0) {
      text.setTextColor(resources.getColor(this.style.textColorResourceId));
    }

我有一个类来管理我的自定义字体,就像这样:public static Typeface getGothamBookBold(Context ctx) { return Typeface.createFromAsset(ctx.getAssets(), BASE + "Gotham-Bold.otf"); } - mrroboaat
你是如何编辑Crouton.class文件的?它被锁定在build/intermediates/exploded-aar/de-key..../crouton/1.8.5/classes.jar中。 - ono
@ono,我不确定我理解你的问题。我没有编辑Crouton源代码。 - Ahmad
@Ahmad,你在编辑Crouton.class中的initializeTextView方法吗? - ono
@ono 啊,抱歉。我没有看到问题的上下文。我两年前回答过这个问题,不记得OP要求一个自定义版本的Crouton。基本上,你需要将Crouton作为模块添加,而不是通过gradle添加依赖项,并自行修改源代码。我刚刚检查了一下,似乎Crouton仍然不支持自定义字体,所以这是其中一个可能的解决方案。可能是最直接的一个。 - Ahmad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接