如何在一个地方为整个Xamarin.Android应用程序设置fontFamily?
我想设置工具栏文本、按钮、文本视图的如何在一个地方为整个Xamarin.Android应用程序设置fontFamily?,xamarin.android,set,font-family,Xamarin.android,Set,Font Family,我想设置工具栏文本、按钮、文本视图的fontFamily,以及Xamarin.Andrid应用程序的所有文本。我如何在一个地方做到这一点?我尝试在style.xml中的基本应用程序主题中添加以下内容: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> ..... <item name="f
fontFamily
,以及Xamarin.Andrid应用程序的所有文本。我如何在一个地方做到这一点?我尝试在style.xml中的基本应用程序主题中添加以下内容:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
.....
<item name="fontFamily">@drawable/Exo2_ExtraBold</item>
.....
</style>
.....
@可拉伸/Exo2_超粗体
.....
但它只改变了main活动的fontframy
,我想这是因为只有该活动在其上方具有Theme=“@style/AppTheme”
属性。如果我把这个属性放在其他活动之上,我想它也会起作用,但是不是有一种更简单的方法来实现所有应用程序,在应用程序的单个位置设置一个fontFamily
?我还将.otf
文件放在drawables
文件夹中,我尝试将其放在手动创建的font
文件夹中,但在尝试重建解决方案时出错。因此,我想知道如何解决这个问题,将.oft
放在正确的文件夹中。当我们创建一个新的活动
时,它的超类默认为活动
。因此,我们应该手动将其更改为AppCompatActivity
。并添加行Theme=“@style/AppTheme”
在android中使用自定义字体的最简单方法是使用,对于Xamarin
,您可以使用
将您的自定义字体(例如:my custom font.ttf
)复制到font
目录中的assets
目录中
添加nuget软件包安装软件包CallygraphyXamarin
创建一个类,例如:Startup
,并向其中添加以下内容:
[应用程序]
公共类启动:应用程序
{
公共启动(IntPtr javaReference,JniHandleOwnership transfer)
:base(javaReference,transfer){}
public override void OnCreate()
{
base.OnCreate();
CalligraphyConfig.InitDefault(
新建CalligraphyConfig.Builder()
.SetDefaultFontPath(“字体/我的自定义字体.ttf”)
.SetFontAttrId(Resource.Attribute.fontPath)
.Build()
);
}
}
在您的活动中
添加以下代码,您就可以开始了
protected override void AttachBaseContext(上下文上下文)
{
base.AttachBaseContext(书法.书法ContextWrapper.Wrap(上下文));
}
您可以创建一个基本活动,以避免向每个活动添加上述代码
更新:
为了更改工具栏的字体,如果不想将Textview
添加为标题,则应将以下样式添加到style.xml
字体/custom-font.ttf
并将此样式应用于工具栏,如下所示:
这是我的主要活动
:
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : AppCompatActivity
{
Button informationBtn;
ImageButton congressImageBtn;
ImageButton exhibitionAreaImageBtn;
ImageButton facebookImageBtn;
ImageButton twitterImageBtn;
ImageButton linkedinImageBtn;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainActivity);
FindViews();
BindClickEvents();
}
/// <summary>
/// Method to find the views
/// </summary>
private void FindViews()
{
informationBtn = FindViewById<Button>(Resource.Id.informationBtn);
congressImageBtn = FindViewById<ImageButton>(Resource.Id.congressImageBtn);
exhibitionAreaImageBtn = FindViewById<ImageButton>(Resource.Id.exhibitionAreaImageBtn);
facebookImageBtn = FindViewById<ImageButton>(Resource.Id.facebookImageBtn);
twitterImageBtn = FindViewById<ImageButton>(Resource.Id.twitterImageBtn);
linkedinImageBtn = FindViewById<ImageButton>(Resource.Id.linkedinImageBtn);
}
/// <summary>
/// Method to bind the click events
/// </summary>
private void BindClickEvents()
{
informationBtn.Click += delegate
{
StartActivity(typeof(ContactInformationActivity));
Finish();
};
congressImageBtn.Click += delegate
{
StartActivity(typeof(CongressActivity));
Finish();
};
exhibitionAreaImageBtn.Click += delegate
{
StartActivity(typeof(ExhibitionAreaActivity));
Finish();
};
facebookImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://www.facebook.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
twitterImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://twitter.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
linkedinImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://www.linkedin.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
}
protected override void AttachBaseContext(Context context)
{
base.AttachBaseContext(Calligraphy.Xamarin.CalligraphyContextWrapper.Wrap(context));
}
}
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true,ScreenOrientation=ScreenOrientation.grait)]
公共类MainActivity:AppCompativeActivity
{
按钮信息;
图像按钮一致性图像BTN;
图像按钮显示区域图像BTN;
ImageButton facebookImageBtn;
ImageButton twitterImageBtn;
ImageButton链接到ImageBTN;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainActivity);
FindView();
BindClickEvents();
}
///
///方法来查找视图
///
私有void FindViews()
{
informationBtn=findviewbyd(Resource.Id.informationBtn);
congressImageBtn=FindViewById(Resource.Id.congressImageBtn);
ExpressionAreaImageBTN=FindViewById(Resource.Id.ExpressionAreaImageBTN);
facebookImageBtn=FindViewById(Resource.Id.facebookImageBtn);
twitterImageBtn=FindViewById(Resource.Id.twitterImageBtn);
linkedinImageBtn=findviewbyd(Resource.Id.linkedinImageBtn);
}
///
///方法绑定单击事件
///
私有void BindClickEvents()
{
信息BTN.单击+=委派
{
StartActivity(类型(ContactInformationActivity));
完成();
};
congressImageBtn.单击+=委派
{
StartActivity(typeof(CongressActivity));
完成();
};
ExpressionAreaImageBTN.单击+=委派
{
StartActivity(类型(Exhionareactivity));
完成();
};
facebookImageBtn.单击+=委派
{
var uri=Android.Net.uri.Parse(“https://www.facebook.com/");
var intent=新的intent(intent.ActionView,uri);
星触觉(意向);
};
twitterImageBtn。单击+=委派
{
var uri=Android.Net.uri.Parse(“https://twitter.com/");
var intent=新的intent(intent.ActionView,uri);
星触觉(意向);
};
linkedinImageBtn。单击+=委托
{
var uri=Android.Net.uri.Parse(“https://www.linkedin.com/");
var intent=新的intent(intent.ActionView,uri);
星触觉(意向);
};
}
受保护的覆盖无效AttachBaseContext(上下文上下文)
{
base.AttachBaseContext(书法.Xamarin.CalligraphyContextWrapper.Wrap(上下文));
}
}
默认情况下,我的所有活动都继承自AppCompatActivity
。因此,我必须在所有活动之前添加Theme=“@style/AppTheme”
?没有其他更短的方法了?也许您可以创建一个BaseActivity
。然后将主题=“@style/AppTheme”
放在上面?是的,您可以试试。如果它不起作用,可能您必须在每个活动中添加它
[Activity(Label = "Activity1", Theme = "@style/AppTheme")]
public class Activity1 : AppCompatActivity
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : AppCompatActivity
{
Button informationBtn;
ImageButton congressImageBtn;
ImageButton exhibitionAreaImageBtn;
ImageButton facebookImageBtn;
ImageButton twitterImageBtn;
ImageButton linkedinImageBtn;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainActivity);
FindViews();
BindClickEvents();
}
/// <summary>
/// Method to find the views
/// </summary>
private void FindViews()
{
informationBtn = FindViewById<Button>(Resource.Id.informationBtn);
congressImageBtn = FindViewById<ImageButton>(Resource.Id.congressImageBtn);
exhibitionAreaImageBtn = FindViewById<ImageButton>(Resource.Id.exhibitionAreaImageBtn);
facebookImageBtn = FindViewById<ImageButton>(Resource.Id.facebookImageBtn);
twitterImageBtn = FindViewById<ImageButton>(Resource.Id.twitterImageBtn);
linkedinImageBtn = FindViewById<ImageButton>(Resource.Id.linkedinImageBtn);
}
/// <summary>
/// Method to bind the click events
/// </summary>
private void BindClickEvents()
{
informationBtn.Click += delegate
{
StartActivity(typeof(ContactInformationActivity));
Finish();
};
congressImageBtn.Click += delegate
{
StartActivity(typeof(CongressActivity));
Finish();
};
exhibitionAreaImageBtn.Click += delegate
{
StartActivity(typeof(ExhibitionAreaActivity));
Finish();
};
facebookImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://www.facebook.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
twitterImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://twitter.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
linkedinImageBtn.Click += delegate
{
var uri = Android.Net.Uri.Parse("https://www.linkedin.com/");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
};
}
protected override void AttachBaseContext(Context context)
{
base.AttachBaseContext(Calligraphy.Xamarin.CalligraphyContextWrapper.Wrap(context));
}
}