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

在Android应用中,当尝试将RobotoLight Italic字体应用于TextView时,发现TextView的高度变为实际文本高度的5倍。问题源于特定字体的使用。通过在TextView添加布局变更监听器并重新设置高度来临时修复此问题,但这不是理想解决方案。代码片段展示了如何通过监听器调整高度。问题可能由字体文件或布局参数引起,需要进一步调查以找到更优雅的解决办法。
摘要由CSDN通过智能技术生成

我正在尝试在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(第二个)

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">

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" />

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" />

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值