这已经被问了很多,但是还没有被要求使用xamarin。
这是我的问题:
我有一个用于自定义微调器的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xy1z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:paddingLeft="20dp" />
我有一个自定义微调器:
spnMyFriends = FindViewById<Spinner>(Resource.Id.txt_myfriends_addafriend);
String[] friends = new String[]
{
"My friends",
"I am following",
};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, Resource.Layout.spinner_item, friends);
spinnerArrayAdapter.SetDropDownViewResource(Resource.Layout.spinner_item);
spnMyFriends.Adapter = spinnerArrayAdapter;
spnMyFriends.ItemSelected += new EventHandler
<AdapterView.ItemSelectedEventArgs>(SpinnerItems);
现在,您会认为,由于我可以编辑xml文件,因此只需执行以下操作:
Textview txtv = FindViewById<TextView>(Resource.Id.xy1z);
txtv.SetTypeFace(font, TypefaceStyle.Normal);
这适用于每个单文本视图-但是不适用于我的微调器。最后一行,我设置字体的行因“ ...未设置对象实例”而崩溃。当然了,这是错误的,因为我在那之前的行中初始化了Textview对象。
1.)为什么我的想法无效?
2.)如何更改字体?
帮助会很棒。
该Spinner
是适配器模式。您在错误的位置设置字体。
这适用于每个单文本视图。
是的,您可以TextView
在“活动”的布局中找到一个“ a”并配置其字体,因为“活动”需要使用布局来显示其内容。
该Spinner
也有布局,你可以看到它是一个弹出窗口,其内容的需要Adapter
来配置,所以在Adapter
你可以得到TextView
并设置其字体。
就像@elmorabea所说的那样,您需要一个自定义Adapter
,在GetView
方法中,您需要设置Typeface
,下面是一个简单的例子:
public class MainActivity : Activity
{
Spinner spnMyFriends;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
initView();
}
private void initView()
{
spnMyFriends = FindViewById<Spinner>(Resource.Id.txt_myfriends_addafriend);
List<string> friends = new List<string>
{
"My friends",
"I am following",
};
MyAdapter myAdapter = new MyAdapter(this,friends);
spnMyFriends.Adapter = myAdapter;
}
}
public class MyAdapter : BaseAdapter<string>
{
Context mContext;
readonly LayoutInflater inflater;
List<string> itemList;
public MyAdapter(Context context, List<string> list) {
this.inflater = LayoutInflater.FromContext(context);
this.mContext = context;
this.itemList = list;
}
public override string this[int position]
{
get { return itemList[position]; }
}
public override int Count {
get { return itemList.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? inflater.Inflate(Resource.Layout.spinner_item, parent, false);
var item = itemList[position];
TextView tv = view.FindViewById<TextView>(Resource.Id.xy1z);
// here to set your Typeface
Typeface typeFace1 = Typeface.CreateFromAsset(mContext.Assets, "fonts/NotoSansCJK-Black.ttc");
tv.SetTypeface(typeFace1, TypefaceStyle.Normal);
tv.Text = itemList[position];
return view;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句