简体   繁体   English

Xamarin.Forms FindViewById() 返回 null

[英]Xamarin.Forms FindViewById() Returns null

I am trying to create a toast with a custom font, in my layout file in the android project i did:我正在尝试在 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>

And then for the custom toast I did:然后对于我做的自定义吐司:

        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();

But then at runtime, the textview v is always null.但是在运行时,textview v 总是 null。

How do I find my textview with my custom font by its ID?如何通过 ID 找到带有自定义字体的 textview?

THank you!谢谢你!

as far as i know the textview id from toast is: android.R.id.message据我所知,烤面包的 textview id 是:android.R.id.message

From your description, you want to create Toast with custom font in Xamarin.forms, you can use DependencyService to get it.根据您的描述,您想在 Xamarin.forms 中使用自定义字体创建 Toast,您可以使用DependencyService来获取它。

In your Share code project, create interface toast:在您的 Share 代码项目中,创建接口 toast:

public interface toast
{
    void Show(string message);
}

Then implement this interface in Android platform.然后在Android平台上实现这个接口。 you can use following code to create custom font Toast.Don't forget to register the platform implementations.您可以使用以下代码创建自定义字体 Toast。不要忘记注册平台实现。

[assembly: Xamarin.Forms.Dependency(typeof(Toast_Android))]
namespace FormsSample.Droid
{
public class Toast_Android : toast
{
    public void Show(string message)
    {
        
        Toast toast = Toast.MakeText(Application.Context, message, ToastLength.Long);
        TextView v = (TextView)toast.View.FindViewById(Android.Resource.Id.Message);
        v.SetTextSize(Android.Util.ComplexUnitType.Sp, 25);
        toast.View.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#F2e3dace"), PorterDuff.Mode.SrcIn);

        var typeface = Typeface.Create("customfont", Android.Graphics.TypefaceStyle.Bold);
        v.Typeface = typeface;

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


    }
}
 }

For Android native platform, there is one textview from Toast: Android.Resource.Id.Message对于Android原生平台,Toast有一个textview: Android.Resource.Id.Message

Then you can get Toast by Button.click method:然后就可以通过 Button.click 方法获取 Toast:

private void btn2_Clicked(object sender, EventArgs e)
    {
        DependencyService.Get<toast>().Show("Toast Message");
    }

The screenshot:截图:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM