试试这个。添加如下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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/custom_toast_container"
android:background="@drawable/complete_round_light_green_fill">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginHorizontal="24dp"
android:layout_marginVertical="15dp"
android:layout_gravity="center_horizontal"
android:fontFamily="@font/ubuntu"
android:textSize="14sp"
android:lineSpacingExtra="7sp"
android:textColor="@color/white"
tools:text="abcd"
/>
</LinearLayout>
在Java代码中添加这个。如您所见,我们可以添加背景,将重力设置为土司:
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.layout_toast_green, null);
LinearLayout customContainer = (LinearLayout) layout.findViewById(R.id.custom_toast_container);
customContainer.setBackgroundResource(R.drawable.complete_round_mgred_fill);
TextView text = (TextView) layout.findViewById(R.id.message);
text.setText("Your request is accepted, Please make your payment!");
Toast toast = new Toast(AppController.getContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.setView(layout);
toast.show();
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#78cd96" />
<corners android:radius="100dip"/>
</shape>
</item>
</layer-list>