首页 > 解决方案 > Xamarin.Forms FindViewById() 返回 null

问题描述

我正在尝试在我所做的 android 项目的布局文件中使用自定义字体创建吐司:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView 
        android:id="@+id/toastTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:paddingLeft="15dp"
        android:fontFamily="@font/made_evolve_sans_light"
        android:textColor ="#424242"
        android:textSize="25sp"
    />
</LinearLayout>

然后对于我做的自定义吐司:

        Toast toast = Toast.MakeText(Application.Context, message, ToastLength.Long);
        TextView v = (TextView)toast.View.FindViewById(Resource.Id.toastTextView);
        v.SetTextSize(Android.Util.ComplexUnitType.Sp, 25);
        toast.View.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#F2e3dace"), PorterDuff.Mode.SrcIn);

        toast.SetGravity(GravityFlags.Center, 0, 0);
        toast.Show();

但是在运行时,textview v 始终为空。

如何通过 ID 找到带有自定义字体的 textview?

谢谢你!

标签: androidxamarin.android

解决方案


据我所知,来自 toast 的 textview id 是:android.R.id.message


推荐阅读