【问题标题】:Stroke in textview text android?中风文本视图文本android?
【发布时间】:2015-08-05 06:37:08
【问题描述】:

我需要根据下图为 textview 设置文本样式

使用的字体是 CARTER ONE 字体

这是我的文本视图类

public class CustomTextViewCarter extends TextView {
private static Typeface mTypeface;
public CustomTextViewCarter(final Context context) {
    this(context, null);
}

public CustomTextViewCarter(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);

}

public CustomTextViewCarter(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode()) {
        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getResources().getAssets(), "carter_one.ttf");
        }
        setTypeface(mTypeface);
    }
}

}

但没有出现相同类型的文本。怎么办?

【问题讨论】:

标签: android fonts textview styles stroke


【解决方案1】:

使用此代码显示自定义 TextView 第 1 步: CustomTextView.java

public class CustomTextView extends TextView {
    private static final String TAG = "TextView";

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTV);
        String customFont = a.getString(R.styleable.CustomTV_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }

}

第 2 步: 之后在 styles.xml 文件中写入以下代码

<declare-styleable name="CustomTV">
        <attr name="customFont" format="string"></attr>
</declare-styleable>

第三步: 在您的 xml 文件中使用 CustomTextView 这里 com.app.demo 是我的包名.. 使用你的包名而不是那个,

<com.app.demo.CustomTextView
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/title"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="hello"
     android:textColor="@color/text"
     android:textStyle="bold"
     app:customFont="carter_one.ttf" />

【讨论】:

    【解决方案2】:

    试试这个。 您可以通过制作自己的 textview 实现来设置 ttf。 它适用于 API 15(冰淇淋三明治)以上。

    https://stackoverflow.com/a/5185587/850347

     <com.lht.ui.MyTextView
        android:text="Hello friends"
        lht:ttf_name="ITCBLKAD.TTF"
        />   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多