【发布时间】:2017-10-19 20:49:44
【问题描述】:
TextView 没什么特别的,它只是加载自定义字体:
public class TestTextView extends AppCompatTextView {
public TestTextView(Context context) {
super(context);
init(context);
}
public TestTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TestTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
void init(Context context) {
Typeface t = Typeface.createFromAsset(context.getAssets(), "fonts/daisy.ttf");
setTypeface(t);
}
}
布局也很基础,但以防万一:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/material_red200"
android:orientation="vertical">
<*custompackage* .TestTextView
android:gravity="left"
android:padding="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="just some text for testing"
android:textColor="@color/material_black"
android:textSize="100dp" />
</LinearLayout>
如您所见,左侧的部分,如 'j' 和 'f' 被切断了。
设置内边距或边距无效。
当从其他程序中使用时,此字体适合它的框架。
提前致谢。
编辑: @play_err_ 提到的不是我的解决方案。
- 我在最终版本中使用了一个自动调整大小的文本视图,因此添加空格会非常困难。
- 我需要解释一下为什么其他程序(例如 Photoshop、After Effects...)可以计算出合适的边界框而 android 不能
-
我也在动态加载不同的字体,我不想创建一个
if(badfont) addSpaces()
【问题讨论】:
-
尝试删除
android:padding="0dp" -
@AlexanderTumanin 这对结果没有任何影响。
-
尝试在最后一个字符后添加一个空格或为 TestTextView 设置一个固定宽度。
-
字体文件可以共享吗?是否应用了任何自定义文本样式和边框?
-
@AmitKumar 这是我使用的字体:1001fonts.com/daisy-script-font.html