0
我有一个尝试创建自定义Toast对象的活动。我希望烤面包出现在屏幕的中央,但无论我设定的重力是什么,烤面包都会出现在顶部。看起来我给敬酒的设置对发生的事情没有影响。我能做些什么来将敬酒移到中心。为什么SetGravity onToast没有效果?
的代码来创建敬酒:
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.notification_layout,null);
((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.content)).setText(content);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(layout);
toast.show();
的布局膨胀的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="100dp"
android:id="@+id/notificationToast">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/imageView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="@+id/title"
android:gravity="center"
android:layout_alignBottom="@+id/imageView4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/imageView4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="content"
android:id="@+id/content"
android:layout_below="@+id/imageView4"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
愚蠢的小失误总是最长的发现。谢谢 – nbroeking
发生在我们身上 –