
[英]How do I use EventListener in BroadcastReceiver with custom Action in Xamarin.Android?
[英]How do I use a custom font in Xamarin.Android?
我了解如何将 .tff 文件添加到我的项目中,但我一直无法找到一篇文章来说明一旦添加字体后如何以及在何处实现。 我发现一个帖子说要实现这样的字体:
var typeface = Typeface.CreateFromAsset (context.Assets, fileName);
但是我不知道我应该在哪里添加这行代码,或者我应该使用什么来代替上下文。 有人可以给我一个基本的解释,所以我知道我错过了什么吗?
使用模板创建的Xamarin.Android
单个 Activity 应用程序:
使用AndroidAsset
的构建类型将字体添加到 Assets 目录:
├── Assets
│ ├── AboutAssets.txt
│ └── Jellee-Roman.ttf
在OnCreate
添加以下内容:
Button button = FindViewById<Button>(Resource.Id.myButton);
// Add these two lines:
var font = Typeface.CreateFromAsset(Assets, "Jellee-Roman.ttf");
button.Typeface = font;
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
获得对Typeface
的引用后,您可以通过以下方式将其设置到TextView
:
Typeface
属性 - https://developer.xamarin.com/api/property/Android.Widget.TextView.Typeface/
SetTypeface
方法 - https://developer.xamarin.com/api/member/Android.Widget.TextView.SetTypeface/p/Android.Graphics.Typeface/Android.Graphics.TypefaceStyle/
如果你想改变一个 textview 的字体,那么在你的OnCreate
方法中把它放在你执行 FindViewById 之后:
var typeface = Typeface.CreateFromAsset(Assets, "filename.ttf");
myTextView.TypeFace = typeface;
只是为了通用,并用作样式,您可以在 .axml 中执行此操作(字体需要进入“Resources/Font/PermanentMarker.ttf”):
<TextView
...
android:fontFamily="@font/permanentmarker"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.