字体在Android Studio预览中更改,但在设备中未更改

时间:2018-07-31 15:00:37

标签: android text coding-style styles

我创建了一种应用于TextView的样式。它按我的意愿出现在Android Studio预览中,但在设备上却有所不同。我该怎么办?

这是XML代码:

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

    <TextView
    style="@style/smalltextstyle"
        android:text="text here "
     />
</ScrollView>

这是我的样式代码:

<style name="smalltextstyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
    <item name="android:textSize">23sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>

这就是必须的

Expected appearance

这就是出现的

Actual appearance

1 个答案:

答案 0 :(得分:0)

将此类用于您的 customTextview ,并在 assets文件夹中下载字体

xml

例如:

 <com.yourpackagename.font.MyTextView
                android:id="@+id/text_phone_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_phone_number"
                android:textColor="@color/colorDarkAsh_opacity"
                android:layout_marginTop="20dp"
                android:layout_marginStart="10dp"
                android:textSize="12sp"/> 

课程

public class MyTextView extends AppCompatTextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf;

            switch (getTypeface().getStyle()) {
                case Typeface.BOLD:
                    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMedium.otf");
                    break;

                case Typeface.ITALIC:
                    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTBookOblique.otf");
                    break;

                case Typeface.BOLD_ITALIC:
                    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMediumOblique.otf");
                    break;

                case Typeface.NORMAL:
                    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
                    break;

                default:
                    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
                    break;

            }

            setTypeface(tf);
        }
    }

}