Android 自定义toast 宽高大小 背景图片
RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_custom_toast,null);
((TextView) layout.findViewById(R.id.tvCheckoutWay)).setText("11111");
((TextView) layout.findViewById(R.id.tvPercent)).setText("22222");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM, 0,(int)getResources().getDimension(R.dimen.spacing_4));
toast.setView(layout);//setting the view of custom toast layout
toast.show();
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
style="@style/style_base"
android:layout_width="200dp"
android:layout_height="48dp"
android:background="@drawable/bg_toast_data"
android:gravity="center"
>
<TextView
android:id="@+id/tvCheckoutWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="AAAA" />
<TextView
android:id="@+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="{100%}"
android:textColor="@color/green_deep"
android:textSize="@dimen/text_size_menu" />
</LinearLayout>
</RelativeLayout>
需要注意的是最外一层layout不要放背景图片,不然怎么都改变不了大小和宽高,要改只能在中间这层再改
图片