ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 当我使用Roboto-Italic.ttf字体时,为什么TextView.setTypeface()夸大地增加了我的TextView高度?

2019-07-08 20:25:51  阅读:274  来源: 互联网

标签:android android-layout textview fonts android-textview


我正在尝试在TextView上设置Roboto Light字体.
我从here下载了Roboto字体.

我从代码中做的就是:

sRobotoItalic = Typeface
        .createFromAsset(getContext().getAssets(), "fonts/Roboto_v1.2/Roboto-Italic.ttf");
final TextView textView = (TextView) view.findViewById(R.id.text_view);
textView.setTypeface(sRobotoItalic);

并且我的TextView的高度几乎变为实际文本高度的5倍.

我确定这是我的TextView,而不是其他一些视图来改变它的大小(使用背景颜色)和那个特定的字体导致问题(我试图在我的TextView上设置不同的字体,当我使用时没有错他们).

现在,我找到了解决这个问题的方法

sRobotoItalic = Typeface
        .createFromAsset(getContext().getAssets(), "fonts/Roboto_v1.2/Roboto-Italic.ttf");
final TextView textView = (TextView) view.findViewById(R.id.text_view);
    textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            int height = v.getHeight();
            mTypefaceUtils.setRobotoItalicTypeFace(passedExamsLabel);
            ((TextView) v).setHeight(height);
        }
    });

但这更像是一个解决方案.如果有更多文本视图具有同样的问题,代码会很快变得混乱.

这是TextView xml(第二个)

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/marginBottom"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="@dimen/marginTop"
                android:padding="4dp">

                <TextView
                    android:id="@+id/text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="5dp"
                    android:textStyle="italic"
                    android:textSize="@dimen/text_size" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="10dp"
                    android:text="0"
                    android:textSize="@dimen/text_size"
                    android:textStyle="italic" />
            </RelativeLayout>

难道我做错了什么?我该怎么修?

解决方法:

好吧,不要使用谷歌提供的字体,但那些:https://github.com/android/platform_frameworks_base/tree/master/data/fonts

我从手机中取出字体(N5,5.0),效果很好.

标签:android,android-layout,textview,fonts,android-textview
来源: https://codeday.me/bug/20190708/1405794.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有