我无法在屏幕的左上角定位自定义吐司。出于某种原因,存在差距,我猜测差距是为状态栏保留的。但我隐藏了状态栏。
这是我的自定义吐司类。你可以看到我将引力设置在左上角。
class CustomToast : Toast
{
public CustomToast(Context context)
: base(context)
{
}
public CustomToast(Context context, Activity activity, string message, string title/*, bool flag*/)
: base(context)
{
LayoutInflater inflater = activity.LayoutInflater;
View view = inflater.Inflate(Resource.Layout.banner, null);
TextView header = view.FindViewById<TextView>(Resource.Id.txtCustomToastTitle);
TextView body = view.FindViewById<TextView>(Resource.Id.txtCustomToast);
header.Text = title;
body.Text = message;
SetGravity(GravityFlags.Top | GravityFlags.Left, 0, 0);
Duration = ToastLength.Long;
View = view;
}
}
这是我的布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop ="6dp"
android:orientation="vertical"
android:background="@drawable/notificationbackground"
android:layout_gravity="left|top">
<TextView
android:id="@+id/txtCustomToastTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView
android:id="@+id/txtCustomToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
编辑:我将我的应用设置为全屏模式。这是我的oncreate方法
protected override void OnCreate(Bundle bundle)
{
instance = this; // Push notification test code
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
LoadApplication(new App());
RegisterWithGCM();
}
更新:我认为为什么它不让我在状态栏区域中放置吐司(即使我的应用程序处于全屏模式)是因为无论我的应用程序是在前台还是后台,都会显示toast。当我的应用程序处于后台时,吐司将覆盖谷歌可能不想要的状态栏。