android ---- Toast 图像+文字显示

android开发中也有类似于web开发中的提示方法,那就是Toast,图片如下,当点击后出现的提示框,但是如果你是一个有梦想有追求的程序员,一定不止于只是这样朴实无华的黑底白字的提示,那么今天小编就根据国外大神的代码进行分析和展示。

带有图片,背景,好看的提示框

这样是不是更好看的,其实这只是一种样式,学会了如何制作你就可以做出各种各样的,下面上代码!

bg_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置透明背景色 -->
    <solid android:color="#BADB66" />
    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="1px"
        android:color="#FFFFFF" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="50px"
        android:bottomRightRadius="50px"
        android:topLeftRadius="50px"
        android:topRightRadius="50px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>  

view_toast_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lly_toast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_toast"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/iv_lol_icon1" />

    <TextView
        android:id="@+id/tv_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />

</LinearLayout>

ButtonActivity.java

      private Button btn_toast;     
      private Context mContext;   

@Override
public void onCreate(Bundle savedInstanceState){

//设置这个值
      mContext = ButtonActivity.this;


     btn_toast = (Button) findViewById(R.id.btn_toast);
     btn_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                midToast("普通用户",Toast.LENGTH_SHORT);
            }
        });
}
 /**
主要的方法
    
     * @param str 显示的文字
     * @param showTime 时间
     */
    private void midToast(String str, int showTime)
    {
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.view_toast_custom,
                (ViewGroup) findViewById(R.id.lly_toast));
        ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
        TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        tv_msg.setText(str);
        Toast toast = new Toast(mContext);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }

activity_button.xml

   <Button
            android:id="@+id/btn_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示Toast" />

下面运行android模拟器看一下结果,

但是这个时候产品经理走过来是说要加需求,普通用户和Vip用户的提示不一样,那么我们苦逼的码农只能改咯,

下面我们加一个提示框

bg_toast2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--圆角边框-->
    <corners android:radius="20dp" />

    <!--背景-->
    <solid android:color="#ff4411" />

    <!--设置边框-->
    <stroke
        android:width="2sp"
        android:color="#000000" />

    <!--设置上下距离-->
    <padding
        android:bottom="20dp"
        android:left="20dp"
        android:right="20dp"
        android:top="20dp" />

</shape>

view_toast_custom2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lly_toast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_toast2"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img_logo2"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/iv_lol_icon1" />

    <TextView
        android:id="@+id/tv_msg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />

</LinearLayout>

下面写一个通用的方法

    private Button btn_toast;
    private Button btn_toast2;
    private Context mContext;



 mContext = ButtonActivity.this;

        Button btn_toast2=findViewById(R.id.btn_toast2);
        btn_toast2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                midToast_C("VIP用户",Toast.LENGTH_SHORT,R.layout.view_toast_custom2, R.id.lly_toast,R.id.img_logo2,R.id.tv_msg2);
            }
        });
        Button btn_toast1=findViewById(R.id.btn_toast);
        btn_toast1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                midToast_C("普通用户",Toast.LENGTH_SHORT,R.layout.view_toast_custom, R.id.lly_toast,R.id.img_logo,R.id.tv_msg);
            }
        });


  /**
     * 通用方法
     * @param str   显示的文字
     * @param showTime  显示的时间
     * @param custom    提示框id
     * @param toast_    内容id
     * @param img_logo_ logo图片id
     * @param tv_msg_   显示文字id
     */
    private void midToast_C(String str, int showTime,int custom ,int toast_, int img_logo_,int tv_msg_)
    {

        System.out.println("str:"+str+"\tshowTime:" + showTime + "\tcustom:" + custom + "\ttoast:" + toast_ + "\timg_logo_:" + img_logo_ + "\ttv_msg:" + tv_msg_);

        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(custom, (ViewGroup) findViewById(toast_));
        ImageView img_logo = (ImageView) view.findViewById(img_logo_);
        TextView tv_msg = (TextView) view.findViewById(tv_msg_);
        tv_msg.setText(str);
        Toast toast = new Toast(mContext);
        toast.setGravity(Gravity.CENTER, 50, 150);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }

猜你喜欢

转载自blog.csdn.net/qq_41426326/article/details/90171435