在res文件夾中我應該放置我的字體文件(TTF)?我應該在Android資源中放置字體文件?
18
A
回答
9
您可以創建資源文件夾的字體(即資產/字體/ roboto.ttf)。
然後,創建您的TextView適當類:
// RobotoFont class
package com.my.font;
public class RobotoFont extends TextView {
public RobotoFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RobotoFont(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RobotoFont(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"));
}
else if(style == Typeface.ITALIC)
{
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
}
else
{
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
}
}
}
最後,更新您的佈局:
//main.xml
//replace textview with package name com.my.font.RobotoFont
<com.my.font.RobotoFont
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="2dip" />
5
不在res文件夾中,而是在資產文件夾中的任意位置。然後你可以從Typeface
使用createFromAsset
靜態方法:
17
使用自定義字體
第一步是選擇一個您要使用的字體。
第二次在資產目錄中創建一個Fonts文件夾,並在那裏複製你的字體。
注:你可以把你的字體上到處都是asssets文件夾但是這是我的方式!
這就是設置,現在到代碼。
要訪問自定義字體,必須使用Android SDK中的Typeface類創建Android可以使用的字體,然後設置需要適當使用自定義字體的任何顯示元素。爲了演示,您可以例如在主屏幕上創建兩個文本視圖,一個使用默認的Android Sans字體,另一個使用自定義字體。佈局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/DefaultFontText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Here is some text." />
<TextView
android:id="@+id/CustomFontText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Here is some text.">
</TextView>
</LinearLayout>
加載和設置自定義字體的代碼也很簡單,如下所示。
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
}
}
你可以看到結果:
2
由於的Android 1.5工作室的。1,您可以:
- 鼠標右鍵點擊你
app
目錄 New
>Folder
(這是靠近列表的底部,很容易錯過)>Assets Folder
- 在大多數情況下,你可以離開文件夾的位置作爲默認>點擊Finish
- 移動文件到新創建的文件夾
assets
3
您可以在resources
中創建fonts
文件夾並直接在xml中使用它,因爲Android O。
查看全新Android O - fonts feature。
相關問題
- 1. 我該如何播放放置在資源文件夾中的SWF文件
- 2. 我該如何打開資源文件在Android中閱讀
- 3. 應該在Android中釋放的系統資源
- 4. 將Visual Studio資源文件放置在「資源」以外的文件夾中?
- 5. Android Studio:我必須在哪裏放置資產和資源?
- 6. 在Java中,包/源層次結構中應該放置哪些資源?
- 7. 我應該在哪裏放置Rails 5.1的中間件文件?
- 8. 我在哪裏放置圖像資源文件? BitmapFactory.decodeFile()
- 9. 我應該在哪裏使用資源文件?
- 10. 爲什麼我應該把外部資源放在html頭部?
- 11. 在這種情況下,我應該擔心釋放資源嗎?
- 12. 我應該叫evhttp_request_free釋放http服務器中的資源嗎?
- 13. 我們應該在資源中存儲格式字符串嗎?
- 14. 我應該將存儲在Android Bundles中的字符串用作資源嗎?
- 15. 在哪個目錄中應該放置我的.php文件
- 16. 如何在應用程序中放置資源文檔目錄
- 17. Android測驗應用程序 - 我應該在哪裏放置sqlite文件?
- 18. 我應該在哪裏放置我的css文件?
- 19. Jython,我應該在哪裏放置我的.py文件
- 20. 我應該在哪裏放置我的chromedriver文件?
- 21. Java文件釋放資源
- 22. 資源內置Android資源中的$ NotFoundException?
- 23. 我在哪裏把圖像放在eclipse資源文件夾中?
- 24. 我應該在Jenkins中放置gradle.properties
- 25. 我應該擔心NIB文件資源的內存管理嗎?
- 26. 在Android中下載資源文件
- 27. 在Android中寫入資源文件
- 28. Android:在JAR中添加資源文件
- 29. 我們可以在android資源文件夾中放置包含40mb的文件夾(圖片)嗎?
- 30. 我應該把我的輸出文件放在源代碼控制中嗎?
最好創建一個通用的自定義TextView(例如稱爲TypeFaceTextView),您可以通過預定義屬性app指定自己的字體:fontFamily – MikeL