我正在爲我的android應用定義樣式XML。我有一些我想使用的TTF文件,我如何設置字體使用這些文件作爲字體,而不是通用的「sans」,「serif」&「monospace」。謝謝在styles.xml中設置特定字體
26
A
回答
47
您只能通過Java代碼使用自定義字體,而不能通過佈局XML或樣式/主題 - 抱歉!
-3
請參閱文檔中的APIDemos示例。
7
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
2
我創建這個有點教程...我希望它是有用的http://spinettaro.blogspot.it/2013/01/custom-font-for-view.html
16
TextViewPlus.java:
package com.example;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
attrs.xml:(在res /值)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.example"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.TextViewPlus
android:id="@+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="@string/showingOffTheNewTypeface"
foo:customFont="saxmono.ttf">
</com.example.TextViewPlus>
</LinearLayout>
你會把 「saxmono.ttf」 在資產文件夾。
0
是的,你可以:)
第1步:創建一個文件夾並將其命名爲「字體」,並在它裏面你的.ttf。 enter image description here
第2步:進入style.xml並執行以下操作: - enter image description here
第3步:在任何UI對象使用的樣式標籤: -
+0
在此之前,首先,您需要更新以更新以支持庫26,這裏是鏈接https://segunfamisa.com/posts/custom-fonts-with-android-support-library – 2018-01-12 18:19:28
相關問題
- 1. 在android中設置特定字體actionBar
- 2. 在styles.xml中設置默認字體大小
- 3. 如何在styles.xml中設置DialogFragment.STYLE_NO_FRAME?
- 4. 使用CSS設置特定字體
- 5. emacs的設置字體集的字體特定的unicode炭
- 6. 如何在Android中爲特定文本視圖設置字體?
- 7. 如何在Vaadin表中爲特定列設置字體大小?
- 8. 在Android中設置固定字體
- 9. 在DWORD中設置特定字節
- 10. 在字節中設置特定位
- 11. 將字體粗體設置爲Flex網格的特定列
- 12. 在Android中設置字體
- 13. 在JTextArea中設置字體
- 14. 在javafx中設置字體
- 15. 在Firemonkey中設置字體
- 16. 設置自定義字體
- 17. 設置自定義字體
- 18. 設置自定義字體
- 19. 將數組中的特定字符串設置爲粗體
- 20. 設置自定義TTF字體在MPDF
- 21. 設置數字中的特定位
- 22. 如何將字體粗體設置爲表格小部件中的特定行
- 23. 字體設置
- 24. 如何在styles.xml中設置TextInputEditText的樣式
- 25. 如何在styles.xml中設置我所有活動的背景
- 26. 在WPF RichTextBox中將特定文本設置爲粗體
- 27. 在Xcode 4.2中使用體系結構特定設置
- 28. 在popup div中重置特定窗體
- 29. 將按鈕的輸出文本設置爲特定的字體
- 30. 特定gtk應用程序的字體設置
非常感謝我很感激。 – NickTFried 2010-04-02 14:34:51
@SergiCastellsaguéMillán:這個問題是爲了使用TTF字體。你鏈接到的答案是任何**,但** TTF字體。 – CommonsWare 2014-01-05 22:26:23
由於這是一箇舊的答案,你知道這是否有更新的版本,@CommonsWare?我也希望能夠通過在''文檔中定義字體來更改字體。 –
2015-08-25 18:32:37