gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:45 25 4
gpt4 key购买 nike

我正在尝试在 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 而不是其他 View 来改变它的大小(已经玩过背景颜色)并且是那个特定的字体导致了这个问题(我试图在我的 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 ,代码很快就会变得困惑。

这是 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 - 当我使用 Roboto-Italic.ttf 字体时,为什么 TextView.setTypeface() 夸大地增加了我的 TextView 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22271897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com