我想将TextView的字体设置为Assets文件夹中的字体。问题代码是" var font = Typeface.CreateFromAsset(Assets," Enter-The-Grid.ttf");"不是第一次使用,而是第二次使用我的代码(红色波浪线出现在" Assets")。
namespace UndergroundSports.Android
{
[Activity]
public class CityPage : Activity
{
Sport[] sports = Sport.Sports;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.SetContentView(Resource.Layout.CityPage);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
Button bttJoin = FindViewById<Button>(Resource.Id.bttJoin);
bttJoin.Click += (sender, e) =>
{
gotoJoinPage();
};
bttJoin.Typeface = font;
ListView lstSports = FindViewById<ListView>(Resource.Id.lstSport);
lstSports.Adapter = new SportsAdapter(this, sports);
lstSports.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
{
Sport selectedFromList = sports[e.Position];
Global.Instance.CurrentSport = selectedFromList;
gotoMembersPage();
};
}
private void gotoJoinPage()
{
var intent = new Intent(this, typeof(JoinPage));
StartActivity(intent);
}
private void gotoMembersPage()
{
var intent = new Intent(this, typeof(MembersPage));
StartActivity(intent);
}
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override Sport this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Length; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
}
}
但是当我尝试创建一个包含该字体的变量时,我收到一条错误告诉我:
无法访问外部类型Android.Content.Context' via nested type
的非静态成员UndergroundSports.Android.CityPage.SportsAdapter&#39; (CS0038)(UndergroundSportsAndroid)&#34;
通过查看相关问题,我认为我需要创建Assets对象的实例或使其成为静态。
我对C#很陌生,并且不太了解发生了什么。如果有人能解释为什么我在代码的这一部分无法访问资产,我将不胜感激。让我感到困惑的部分是我使用完全相同的代码行来访问同一文件中的字体,而不会出现错误。
答案 0 :(得分:2)
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
通过构造函数将您的活动的实例传递给您的适配器,并使用它来访问Assests
public class SportsAdapter : BaseAdapter<Sport>
{
Sport[] items;
Activity context;
public SportsAdapter(Activity context, Sport[] items) : base()
{
this.context = context;
this.items = items;
}
....
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(global::Android.Resource.Layout.SimpleListItem1, null);
TextView txtView = view.FindViewById<TextView>(global::Android.Resource.Id.Text1);
var font = Typeface.CreateFromAsset(context.Assets, "Enter-The-Grid.ttf");
txtView.Text = items[position].Name;
txtView.Gravity = GravityFlags.Center;
txtView.Typeface = font;
return view;
}
}
另外,请确保您的.ttf文件的构建操作设置为AndroidAssests。右边的.tff文件&gt;构建行动&gt; AndroidAsset