【问题标题】:Font gets changed in Android Studio preview but not in the device字体在 Android Studio 预览中被更改,但在设备中没有
【发布时间】:2018-07-31 15:00:36
【问题描述】:

我创建了一个应用于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>

应该是这样的

这就是出现的样子

【问题讨论】:

    标签: android text coding-style styles


    【解决方案1】:

    为您的 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);
            }
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2017-06-30
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多