Android 自定义toast总结:纯文本,带图像,带标题栏及自定义背景文字颜色

来源:互联网 发布:linux 安装rpm命令 编辑:程序博客网 时间:2024/05/16 11:37

      这两天因为项目任务安排,做了一个统一的toast的widget。由于toas在APP中用的非常多,所以也记录一下。

 1.首先,创建不同的shape,用于定义不同形状的toast

     shape1: shape.xml

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 设置背景透明度和颜色 -->    <solid android:color="#cf000000" />    <!-- 设置四个角为弧形 -->    <corners android:radius="23dp" />    <padding        android:left="23dp"        android:right="23dp" /></shape>
      shape2:new_shape.xml

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 设置背景透明度和颜色 -->    <!-- 设置四个角为弧形 -->    <corners android:radius="23dp" />    <padding        android:left="23dp"        android:right="23dp" /></shape>
   shape的类型与内容可以自由设定。

2.创建不同的layout并调用不同的shape

   layout1:custom_toast.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:orientation="vertical">    <TextView        android:id="@+id/toast_text"        android:layout_width="wrap_content"        android:layout_height="37dp"        android:gravity="center_vertical"        android:background="@drawable/shape"        android:drawableLeft="@drawable/icon_pro_xhdpi"        android:drawablePadding="13dp"        android:textColor="#FFFFFF"        android:textSize="14sp"        android:paddingLeft="25dp"/></LinearLayout>
   layout2: toast_custom.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:orientation="vertical">    <TextView        android:id="@+id/toast_text"        android:layout_width="wrap_content"        android:layout_height="37dp"        android:gravity="center_vertical"        android:background="@drawable/shape"        android:textColor="#FFFFFF"        android:textSize="14sp"        android:paddingLeft="25dp"/></LinearLayout>
   layout3: toast_customer.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:orientation="vertical">    <TextView        android:id="@+id/toast_text"        android:layout_width="wrap_content"        android:layout_height="37dp"        android:gravity="center_vertical"        android:background="@drawable/new_shape"        android:textSize="14sp"        android:paddingLeft="25dp"/></LinearLayout>
    layout4: customer_toast.xml (设置标题栏和内容栏,带图片)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@null" >    <RelativeLayout        android:layout_width="140dp"        android:layout_height="80dp"        android:background="@drawable/shape"        android:gravity="center"        android:orientation="vertical"         >        <TextView            android:id="@+id/tvTitleName"            android:layout_margin="8dp"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:text="标题标题"            android:textColor="#ffffff"            android:textSize="14sp" />        <TextView            android:id="@+id/tvContent"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@+id/tvTitleName"            android:drawableLeft="@drawable/icon_pro_xhdpi"            android:drawablePadding="13dp"            android:layout_marginBottom="21dp"            android:text="文字内容"            android:textColor="#ffffff"            android:textSize="14sp"           />    </RelativeLayout></LinearLayout>


3.创建不同类型的toast

   

**   自定义toast,各种类型都有 */public class CustomToast {    public static final int LENGTH_SHORT = Toast.LENGTH_SHORT; //数值为0    public static final int LENGTH_LONG = Toast.LENGTH_LONG;   //数值为1    private GradientDrawable myGrad;    Toast toast;    Context mContext;    TextView toastTextField;    // 带图片的toast    public CustomToast(Context context) {        mContext = context;        toast = new Toast(mContext);        toast.setGravity(Gravity.CENTER, 0, 0);// 位置居中设置        View toastRoot = LayoutInflater.from(context).inflate(R.layout.custom_toast, null);//        View toastRoot = activity.getLayoutInflater().inflate(R.layout.custom_toast, null);        toastTextField = (TextView) toastRoot.findViewById(R.id.toast_text);        Drawable drawable = context.getResources().getDrawable(R.drawable.icon_pro_xhdpi);        //设定图片大小        drawable.setBounds(0,0,33,33);        toastTextField.setCompoundDrawables(drawable,null,null,null);        toast.setView(toastRoot);    }    // 自定义背景文字颜色的纯文字toast    public CustomToast(Context context,@StringRes int strContent, @ColorRes int bgColor, @ColorRes int tvColor) {        mContext = context;        toast = new Toast(mContext);        toast.setGravity(Gravity.CENTER, 0, 0);// 位置居中设置        View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast_customer, null);//        // 设置View的颜色//        toastRoot.setBackgroundColor(context.getResources().getColor(bgColor));//        View toastRoot = activity.getLayoutInflater().inflate(R.layout.custom_toast, null);        toastTextField = (TextView) toastRoot.findViewById(R.id.toast_text);        toastTextField.setText(context.getResources().getString(strContent));        // 设置背景shape颜色        myGrad = (GradientDrawable) toastTextField.getBackground();        myGrad.setColor(context.getResources().getColor(bgColor));//        toastTextField.setBackgroundResource(bgColor);//        toastTextField.setBackgroundColor(context.getResources().getColor(bgColor));        // 设定文本文字颜色        toastTextField.setTextColor(context.getResources().getColor(tvColor));        toast.setView(toastRoot);    }    // 不带图片的纯文字toast    public CustomToast(Context context,Activity activity) {        mContext = context;        toast = new Toast(mContext);        toast.setGravity(Gravity.CENTER, 0, 0);// 位置会比原来的Toast偏上一些        View toastRoot = activity.getLayoutInflater().inflate(R.layout.toast_custom, null);//        View toastRoot = activity.getLayoutInflater().inflate(R.layout.custom_toast, null);        toastTextField = (TextView) toastRoot.findViewById(R.id.toast_text);        toast.setView(toastRoot);    }    //带有标题栏的的toast    public CustomToast(Context context,@StringRes int strId,@StringRes int strId2) {        mContext = context;        toast = new Toast(mContext);        toast.setGravity(Gravity.CENTER, 0, 0);// 位置居中设置        View toastRoot = LayoutInflater.from(context).inflate(R.layout.customer_toast, null);        // 设定标题内容        TextView tvTitleName = (TextView) toastRoot.findViewById(R.id.tvTitleName);        tvTitleName.setText(context.getResources().getString(strId));        // 设定文字内容        TextView tvContent = (TextView) toastRoot.findViewById(R.id.tvContent);        tvContent.setText(context.getResources().getString(strId2));        Drawable drawable = context.getResources().getDrawable(R.drawable.icon_pro_xhdpi);        //设定图片大小        drawable.setBounds(0,0,33,33);        tvContent.setCompoundDrawables(drawable,null,null,null);        toast.setView(toastRoot);    }    public void setDuration(int d) {        toast.setDuration(d);    }    public void setText(String t) {        toastTextField.setText(t);    }    //显示带图片的toast    public static CustomToast makeText(Context context, String text, int duration) {        CustomToast toastCustom = new CustomToast(context);        toastCustom.setText(text);        toastCustom.setDuration(duration);        return toastCustom;    }    // 显示纯文字的toast    public static CustomToast makeText(Context context,Activity activity, String text, int duration) {        CustomToast toastCustom = new CustomToast(context,activity);        toastCustom.setText(text);        toastCustom.setDuration(duration);        return toastCustom;    }    //显示自定义样式的toast    public static CustomToast makeText(Context context,@StringRes int strContent,@ColorRes int bgColor, @ColorRes int tvColor,int duration) {        CustomToast toastCustom = new CustomToast(context,strContent,bgColor,tvColor);        toastCustom.setDuration(duration);        return toastCustom;    }    //显示带有标题栏样式的toast    public static CustomToast makeText(Context context,@StringRes int strTitle,@StringRes int strContent,int duration) {        CustomToast toastCustom = new CustomToast(context,strTitle,strContent);        toastCustom.setDuration(duration);        return toastCustom;    }    public void show() {        toast.show();    }    }

    实现过程中遇到的难点是如何用在实现程序中自行定义layout中TextView的shape颜色,其解决过程如下:

   

    myGrad = (GradientDrawable) toastTextField.getBackground();    myGrad.setColor(context.getResources().getColor(bgColor));

4.测试用例

/***  测试用例 */public class MainActivity extends AppCompatActivity implements View.OnClickListener {//    private CustomToast toast;    private Button button,button2,button3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        button2 = (Button) findViewById(R.id.button2);        button3 = (Button) findViewById(R.id.button3);        button3.setOnClickListener(this);        button2.setOnClickListener(this);        button.setOnClickListener(this);        CustomToast.makeText(getApplicationContext(),R.string.details,R.string.back_cancel,0).show();    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.button:                CustomToast.makeText(getApplicationContext(),"确定",                        0).show();                break;            case R.id.button2:                CustomToast.makeText(getApplicationContext(),R.string.account_bind,R.color.colorPrimary,R.color.colorAccent,0).show();                break;            case R.id.button3:                PhiToast.makeText(getApplicationContext(),R.string.account_manage,R.color.colorPrimaryDark,R.color.colorAccent,0).show();                break;        }    }}

其中用到的layout:activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    >    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" />    <Button        android:text="Button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView"        android:layout_centerHorizontal="true"        android:layout_marginTop="274dp"        android:id="@+id/button" />    <Button        android:text="Button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button"        android:layout_alignLeft="@+id/button"        android:layout_alignStart="@+id/button"        android:layout_marginTop="71dp"        android:id="@+id/button2" />    <Button        android:text="Button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/button2"        android:layout_alignLeft="@+id/button"        android:layout_alignStart="@+id/button"        android:layout_marginBottom="104dp"        android:id="@+id/button3" /></RelativeLayout>







0 0