Hai在这里告诉你如何在sketchware ide中添加自定义字体
first step
制作音乐文件并根据需要为其命名
然后加载您的字体文件
使用音乐文件名重命名您的字体文件
例如: - 如果您的音乐文件名为Jiji.mp3
,则将您的字体文件重命名为同名jiji.mp3
并删除原始音乐文件(以避免重复)
//now load this load onCreate.
//or you can use Preload
String path = getExternalCacheDir() + "/myfont.ttf";
now call our font
//let us copy the font on private storage.
copyFiletoExternalStorage(R.raw.myfont2, "myfont.ttf");
此处将myfont2
更改为您的字体名称(不要使用.mp3)
Now we can load our font
//let us load the font
Typeface myTypeface = Typeface.createFromFile(path);
MyTypeface - our Typeface
现在我们可以将小部件添加到Typeface
//let us set the font to text view
widget.setTypeface(myTypeface);
此处小部件为textview,Checkbox,switch,button,edittext,etc..
示例: -
//I have a button with id button1
button1.setTypeface(myTypeface);
现在将此代码添加到
}
private void copyFiletoExternalStorage(int resourceId, String resourceName){
String pathSDCard = getExternalCacheDir() + "/myfont.ttf";//Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
try{
java.io.InputStream in = getResources().openRawResource(resourceId);
java.io.FileOutputStream out = null;
out = new java.io.FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
一起加入.... 如需更多帮助how to add custom font in sketchware 所有代码一起:
String path = getExternalCacheDir() + "/myfont.ttf";
//let us copy the font on private storage.
copyFiletoExternalStorage(R.raw.myfont2, "myfont.ttf");
//let us load the font
Typeface myTypeface = Typeface.createFromFile(path);
//let us set the font to text view
txt2.setTypeface(myTypeface);
}
private void copyFiletoExternalStorage(int resourceId, String resourceName){
String pathSDCard = getExternalCacheDir() + "/myfont.ttf";//Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
try{
java.io.InputStream in = getResources().openRawResource(resourceId);
java.io.FileOutputStream out = null;
out = new java.io.FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}