将自定义字体设置为微调器

创新媒体

这已经被问了很多,但是还没有被要求使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用自定义对象填充微调器。如何在自定义对象中设置微调器提示

用自定义对象填充微调器。如何在自定义对象中设置微调器提示

创建自定义微调器模型

Android为自定义TextView小部件设置字体

Android:将自定义值设置为NumberPicker

如何使用数据绑定库将自定义对象绑定到微调器布局?

将自定义FXML属性设置为自定义javafx组件的参数

将自定义对象设置为微调框并显示特定属性

带有自定义文本字体和颜色的微调器

为我的所有textview设置自定义字体

如何使自定义微调器成为默认微调器?

将自定义样式设置为android中的自定义视图内的textviews

android通过编程将自定义值设置为自定义textview

将自定义chipDrawable背景设置为Chip

如何在Angular7中全局设置字体大小(将自定义值设置为rem)?

如何将自定义委托设置为UITableViewController?

将自定义域设置为“主要”的好处

为UITableView滑动操作(UIContextualAction)设置自定义字体

如何将自定义tabBar设置为我的自定义TabBarController?

将自定义微调器向右对齐-动作栏兼容

我可以将自定义字体升级为Big Commerce吗?

如何在情节提要中将自定义字体设置为UILabel

如何将自定义标记设置为自定义视图?

在OpenCv中将自定义模型设置为CvSVM

上传器:如何将自定义文件夹名称设置为与用户ID相同?

将自定义字体设置为动态添加的文本视图android

Android“自定义”微调器

将自定义 ActionBar 重力设置为 RIGHT

如何将自定义管理器类参数设置为 required=False