我正在试着在TextView上设置Roboto Light字体。我从here下载了机器人字体。
我对我的代码所做的一切是:
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>我做错了什么吗?我该怎么修理呢?
发布于 2014-11-26 20:51:44
好了,伙计们,不要使用谷歌提供的字体,而是那些:https://github.com/android/platform_frameworks_base/tree/master/data/fonts
我从我的手机(N5,5.0)中提取字体,它工作得很好。
https://stackoverflow.com/questions/22271897
复制相似问题