我一直在谷歌搜索,但还没有找到解决方案.
我知道我可以TextView像这样以编程方式更改字体view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));.
但是,有没有办法直接在xml文件中执行此操作?我找到了android:typeface属性,但它只有"普通","sans","serif"和"monospace"的选项.我怎样才能使用这种字体/assets/fonts/Roboto-Regular.ttf?
谢谢.
我写了一个可以做到这一点的图书馆项目.它被称为Nifty:https://github.com/tom-dignan/nifty
使用nifty,您可以从styles.xml文件中指定资产目录中的字体,或者直接在XML布局中指定属性.它提供了一个自定义,NiftyTextView并NiftyButton继承了其父类的所有功能,但提供了在XML中指定字体的功能.
示例样式:
<style name="MyStyle" parent="@android:style/Theme">
<item name="typeface">my_font_that_is_in_assets.ttf</item>
</style>
Run Code Online (Sandbox Code Playgroud)
示例XML:
<com.tomdignan.nifty.NiftyTextView
android:id="@+id/title"
style="@style/MyStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Run Code Online (Sandbox Code Playgroud)
或者,如果您不使用样式:
<com.tomdignan.nifty.NiftyTextView
xmlns:nifty="http://schemas.tomdignan.com/nifty"
nifty:typeface="my_font_that_is_in_assets.ttf"
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Run Code Online (Sandbox Code Playgroud)
好漂亮
警告:自定义视图无法在布局编辑器中预览.我解决这个问题的方法是使用搜索/替换.
| 归档时间: |
|
| 查看次数: |
10946 次 |
| 最近记录: |