网上搜索了各种教程,什么动态设置啊,什么获取屏幕宽高啊之类的,纯属扯淡,不仅繁琐,而且大多没有效果,自己抠了半天,发现一个最简单的解决办法,下面上代码:
先上JAVA代码:
/**
* 描述:自定义吐司
* 创建人:菜籽
* 创建时间:2017/9/21 上午11:13
* 备注:
*/
public class KonstantToast {
public static int TIME_3500 = 3500;
public static int TIME_2000 = 2000;
public static int TIME_1000 = 1000;
public static Toast makeText(Context context, CharSequence text, @BaseTransientBottomBar.Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflate.inflate(R.layout.sm_layout_toast_konstant, null);
((TextView) view.findViewById(R.id.sm_toast_tips)).setText(text);
result.setDuration(duration);
result.setView(view);
return result;
}
}
再上布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="60dp"
android:background="@drawable/sm_style_bg_dialog"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linear_inside"
android:layout_width="250dp"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="15dp"
android:src="@drawable/sm_img_payment_failed" />
<TextView
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_marginLeft="12dp"
android:background="@color/sm_btn_gray" />
<TextView
android:id="@+id/sm_toast_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:textColor="@color/sm_sub_black"
android:textSize="@dimen/text_size_14" />
</LinearLayout>
</LinearLayout>
注意:控制Toast宽高的属性值在父类的LinearLayout中设置是无效的,必须设置在内部的id为linear_inside的控件上才能生效