[英]Using custom font for a Scientific Calculator app in Android Studio
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:labelFor="@android:color/holo_blue_light" android:clickable="true"> <com.example.girikarnal.trial.CalculatorButtonTextView android:id="@+id/acbutton" android:text="@string/AC" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/calc" android:id="@+id/button2" android:layout_above="@+id/button3" android:layout_alignParentStart="true" android:layout_marginBottom="73dp" /> <com.example.girikarnal.trial.CalculatorButtonButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="@string/xsquare" android:layout_centerVertical="true" android:layout_alignParentStart="true" android:background="#ffbfa2ff" android:clickable="true" android:labelFor="@android:color/black" /> <com.example.girikarnal.trial.CalculatorButtonButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button4" android:text="@string/hyp" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginEnd="84dp" /> </RelativeLayout>
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { CalculatorButtonTextView jtv; Button jb; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); jtv=(CalculatorButtonTextView)findViewById(R.id.acbutton); jb=(Button)findViewById(R.id.button2); Typeface tf; tf=Typeface.createFromAsset(getAssets(),"fonts/casiofont.ttf"); jb.setTypeface(tf); }
public class CalculatorButtonTextView extends TextView { // Constructors public CalculatorButtonTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public CalculatorButtonTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CalculatorButtonTextView(Context context) { super(context); init(); } // This class requires casiofont.ttf to be in the assets/fonts folder private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/casiofont.ttf"); setTypeface(tf); }
public class CalculatorButtonButton extends Button { public CalculatorButtonButton(Context context) { super(context); init(); } public CalculatorButtonButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CalculatorButtonButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public CalculatorButtonButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/casiofont.ttf"); setTypeface(tf); } }
<resources> <string name="app_name">fx-991ES</string> <string name="action_settings">Settings</string> <string name="AC">C</string> <string name="calc">r</string> <string name="xsquare">d</string> <string name="hyp">c</string> </resources>
I am building a scientific calculator in Android Studio and i want to use fonts from a ttf file i got from a casio website, I placed them in the assets/fonts folder as suggested in some in other pages but when i run the app i get the error "native type face cant be made".Also other doubt is there was a file called keyboard supplied with the font file i downloaded ,is this the map key like "c=ac". 我正在Android Studio中构建科学计算器,我想使用从卡西欧网站获得的ttf文件中的字体,按照其他页面中的建议将它们放置在Assets / fonts文件夹中,但是当我运行该应用程序时,我得到了错误“本机类型的脸无法制作”。另外还有一个疑问是我下载的字体文件中有一个叫做键盘的文件,这是地图键,例如“ c = ac”。 I am a newbie please help me.
我是新手,请帮助我。 I have added the link to download the ttf file.
我已经添加了下载ttf文件的链接。 " http://edu.casio.com/education/fontset/ " Download the font for fx-ES plus series
“ http://edu.casio.com/education/fontset/ ”下载fx-ES plus系列的字体
For each Button or TextView (I like to make my buttons using textviews because they are more flexible) you need to set your Typeface. 对于每个Button或TextView(我希望使用textviews来制作按钮,因为它们更加灵活),您需要设置字体。
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/ES03.TTF");
myButton.setTypeface(tf);
Since you have a lot of buttons it might just be easier to make a custom button class that extends TextView: 由于您有很多按钮,因此制作自定义按钮类来扩展TextView可能会更容易:
public class CalculatorButtonTextView extends TextView {
// Constructors
public CalculatorButtonTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CalculatorButtonTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CalculatorButtonTextView(Context context) {
super(context);
init();
}
// This class requires ES03.TTF to be in the assets/fonts folder
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/ES03.TTF");
setTypeface(tf);
}
}
And then in your xml file to do something like this for each button/textview: 然后在您的xml文件中为每个按钮/文本视图执行以下操作:
<RelativeLayout
android:id="@+id/key_41"
style="@style/AppTheme.KeyLayoutStyle"
android:layout_weight="1" >
<com.mycalculatorapp.CalculatorButtonTextView
android:id="@+id/tvKey41"
style="@style/AppTheme.KeyTextStyle"
android:text="@string/key_41" />
</RelativeLayout>
I'm cutting and pasting from another project to save time so the actual styling isn't shown here. 我正在从另一个项目进行剪切和粘贴,以节省时间,因此此处没有显示实际的样式。 Explore that project more to see how I applied the styles and strings if that is a question.
如果有问题,请进一步探索该项目,以了解如何应用样式和字符串。
Finally, using FontForge to view the font you can see that the normal letters and numbers are overwritten with the calculator symbols. 最后,使用FontForge查看字体,您可以看到正常的字母和数字被计算器符号覆盖。
Finally you'll do something like 最后你会做类似的事情
myButton.setText("C"); myButton.setText( “C”);
to get the AC button glyph. 获取AC按钮字形。 (Or better yet do it in your xml files.)
(或者更好的方法是在您的xml文件中执行。)
This is the method I used to display special symbols/glyphs that weren't supported by the system fonts. 这是我用来显示系统字体不支持的特殊符号/字形的方法。
Update 更新
Since it still wasn't working for you I decided to try it out myself. 由于它仍然无法为您服务,因此我决定自己尝试一下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button jb=(Button)findViewById(R.id.button1);
Typeface tf;
tf=Typeface.createFromAsset(getAssets(),"fonts/casio.ttf");
jb.setTypeface(tf);
//jb.setText("Cc");
}
Xml: XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cc"
android:id="@+id/button1"
android:textSize="30dp" />
This worked for me both by setting the text in xml and in code. 通过在xml和代码中设置文本,这对我都有效。 I could see both the AC and the hyp glyphs.
我可以看到AC和hyp字形。 So I think you must have a different problem if you are only getting the capital versions.
因此,我认为,如果仅使用大写版本,您肯定会遇到其他问题。 Try restarting with a fresh project.
尝试重新启动一个新项目。
By the way, you may want to beware of memory leaks if you are instantiating a TypeFace in a custom Button or TextView and create a separate class for the TypeFace as is talked about here . 顺便说一句,你可能想,如果你在实例化自定义按钮或TextView的字样提防内存泄漏和字样创建一个单独的类如讲到这里 。 If you don't make a custom class you can just apply the same typeface to all your buttons in onCreate.
如果您不创建自定义类,则可以将相同的字体应用于onCreate中的所有按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.