zme*_*eda 5 fonts android font-family typeface
我知道如何在Android应用程序中以编程方式设置自定义字体。有什么方法可以为自定义字体(资产)加载字体,Android框架将使用基于粗体,斜体等的正确文件吗?
例如现在我正在尝试将Roboto字体设置为一些 TextView
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);
Run Code Online (Sandbox Code Playgroud)
可以。但是由于我将TextViewxml布局内部设置为粗体,所以文本未加粗
<TextView
android:id="@+id/my_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:gravity="center"
android:text="@string/my_text"
android:textColor="@color/my_foreground"
android:textSize="24dp" />
Run Code Online (Sandbox Code Playgroud)
如何正确从资产加载字体,这将起作用?
textView.setTypeface(typeface, Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)
在我的资产目录中,只有一个“字体家族”
Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf
Run Code Online (Sandbox Code Playgroud)
如何在一个字体/家族中加载所有这些字体?
我不知道如何将单一字体的不同字体变体视为一个系列,但字体往往具有较大的文件大小,因此您可能不想将所有这些字体导入到您的应用程序中。相反,您可以仅使用中字体,然后为其设置粗体和斜体属性。
例如,如果您在 XML 布局中设置了 android:textStyle="bold",则可以在代码中执行此操作以保持粗体样式:
Typeface currentTypeFace = textView.getTypeface();
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
textView.setTypeface(tf, Typeface.BOLD);
} else {
textView.setTypeface(tf);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13650 次 |
| 最近记录: |