在 Android Studio 中为科学计算器应用程序使用自定义字体
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>
我正在 Android Studio 中构建一个科学计算器,我想使用从 casio 网站获得的 ttf 文件中的字体,我按照一些建议将它们放在 assets/fonts 文件夹中在其他页面中,但是当我 运行 应用程序时,我收到错误 "native type face cant be made"。还有一个疑问是我下载的字体文件中有一个名为键盘的文件,这是像 [=52 这样的地图键吗? =].我是新手,请帮助我。
我添加了 link 来下载 ttf 文件。
“http://edu.casio.com/education/fontset/”
下载 fx-ES plus 系列
的字体
对于每个 Button 或 TextView(我喜欢使用 textview 制作我的按钮,因为它们更灵活)你需要设置你的字体。
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/ES03.TTF");
myButton.setTypeface(tf);
由于您有很多按钮,制作一个扩展 TextView 的自定义按钮 class 可能会更容易:
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);
}
}
然后在你的 xml 文件中为每个 button/textview:
做这样的事情
<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>
我正在从 another project 中剪切和粘贴以节省时间,因此此处未显示实际样式。如果这是一个问题,请进一步探索该项目以了解我如何应用样式和字符串。
最后用FontForge查看字体可以看到正常的字母和数字都被计算器符号覆盖了。
最后你会做类似
myButton.setText("C");
获取 AC 按钮字形。 (或者最好在 xml 文件中进行。)
这是我用来显示系统字体不支持的特殊symbols/glyphs的方法。
更新
由于它仍然不适合您,我决定自己尝试一下。
@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:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cc"
android:id="@+id/button1"
android:textSize="30dp" />
通过在 xml 和代码中设置文本,这对我很有用。我可以看到 AC 和 hyp 字形。所以我认为如果你只得到大写版本,你一定有不同的问题。尝试使用新项目重新启动。
顺便说一下,如果您在自定义 Button 或 TextView 中实例化 TypeFace 并为 TypeFace 创建一个单独的 class ,如所讨论的那样,您可能需要提防内存泄漏 here .如果您不自定义 class,您可以在 onCreate 中将相同的字体应用于所有按钮。
<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>
我正在 Android Studio 中构建一个科学计算器,我想使用从 casio 网站获得的 ttf 文件中的字体,我按照一些建议将它们放在 assets/fonts 文件夹中在其他页面中,但是当我 运行 应用程序时,我收到错误 "native type face cant be made"。还有一个疑问是我下载的字体文件中有一个名为键盘的文件,这是像 [=52 这样的地图键吗? =].我是新手,请帮助我。 我添加了 link 来下载 ttf 文件。 “http://edu.casio.com/education/fontset/” 下载 fx-ES plus 系列
的字体对于每个 Button 或 TextView(我喜欢使用 textview 制作我的按钮,因为它们更灵活)你需要设置你的字体。
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/ES03.TTF");
myButton.setTypeface(tf);
由于您有很多按钮,制作一个扩展 TextView 的自定义按钮 class 可能会更容易:
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);
}
}
然后在你的 xml 文件中为每个 button/textview:
做这样的事情<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>
我正在从 another project 中剪切和粘贴以节省时间,因此此处未显示实际样式。如果这是一个问题,请进一步探索该项目以了解我如何应用样式和字符串。
最后用FontForge查看字体可以看到正常的字母和数字都被计算器符号覆盖了。
最后你会做类似
myButton.setText("C");
获取 AC 按钮字形。 (或者最好在 xml 文件中进行。)
这是我用来显示系统字体不支持的特殊symbols/glyphs的方法。
更新
由于它仍然不适合您,我决定自己尝试一下。
@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:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cc"
android:id="@+id/button1"
android:textSize="30dp" />
通过在 xml 和代码中设置文本,这对我很有用。我可以看到 AC 和 hyp 字形。所以我认为如果你只得到大写版本,你一定有不同的问题。尝试使用新项目重新启动。
顺便说一下,如果您在自定义 Button 或 TextView 中实例化 TypeFace 并为 TypeFace 创建一个单独的 class ,如所讨论的那样,您可能需要提防内存泄漏 here .如果您不自定义 class,您可以在 onCreate 中将相同的字体应用于所有按钮。