Android 自定义消息提示 toast提示

效果图:

 

 

 

使用toast提示一般有两种方法:

1.调用toast构造器

2.Toast.makeText方法

第二种常见,但不美观。

toast里有个方法:setView    可以让开发者自定义提示的内容

首先,定义一个布局文件(包括一个imageview,一个textview)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_diy_toast"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/diy_toast_bg"          为其设置背景xml
    android:gravity="center">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id_toast_icon"
        android:src="@drawable/icon_toast"
        >

    </ImageView>
    <TextView
        android:id="@+id/id_toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="非法操作!"
        android:textSize="36sp"
        >

    </TextView>
</LinearLayout>

在drawable文件夹下新建一个背景xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置背景色 -->
    <solid android:color="#BADB66" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="20px"
        android:bottomRightRadius="20px"
        android:topLeftRadius="20px"
        android:topRightRadius="20px" />
    <!-- 设置一下边距-->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

java文件

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diy_toast_);

        recommend=findViewById(R.id.id_recommend);
        recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                my_diy_toast("违法操作!", Toast.LENGTH_LONG);         // 定义一个方法
            }

            private void my_diy_toast(String s, int showtime) {
                View view=getLayoutInflater().inflate(R.layout.my_toast,(ViewGroup)findViewById(R.id.id_diy_toast));      // 将定义好的xml布局文件渲染成一个view   ,第二个参数里的id是LinearLayout的id
                TextView textView=view.findViewById(R.id.id_toast_text);
                textView.setText(s);
                Toast toast=new Toast(Diy_toast_Activity.this);
                toast.setDuration(showtime);
                toast.setGravity(Gravity.CENTER,0,0);                   // 设置显示位置
                toast.setView(view);                                                 // 最重要的一句
                toast.show();

            }
        });

    }

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值