<分区>
我有这个 android 代码,其中包含许多类,每个类都有不同的 View 。我们可以转到设置并根据所选字体更改其字体。目前只有预装的 android 字体可用。有没有办法稍微调整我的代码,以便我可以添加一个 .ttf 文件并将其作为字体选项提供。 我不想对代码进行大的更改。
<分区>
我有这个 android 代码,其中包含许多类,每个类都有不同的 View 。我们可以转到设置并根据所选字体更改其字体。目前只有预装的 android 字体可用。有没有办法稍微调整我的代码,以便我可以添加一个 .ttf 文件并将其作为字体选项提供。 我不想对代码进行大的更改。
最佳答案
您可以使用 typeface 为 textview 中的文本设置自定义字体。因此,无论何时您需要为您的 TextView 自定义字体,您都可以使用以下内容。
activity_main.xml
<?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"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
代码:
public class MainActivity extends Activity {
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v) {
text.setText("This is a custom toast");
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/kn.ttf");
text.setTypeface(typeFace);
}
});
}
}
关于android - android 中的自定义字体(适用于整个应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991521/