Android进阶之路 - 自定义Toast,我们让她更美一些吧

每一个故事的完结,或许只是另一个故事起始。
- 望一切安好
Toast在我们开发中,经常用到,不论是在慌乱无知的新手阶段,还是在刹那之间的成长阶段,自定义Toask的需求,其实并没有那么大,更多的或许只是对技术的好奇,同时也是为了让我们的孩子更加完善一些。

自定义的Toast,好比我们平时自己写的最初级的自定义控件,它也需要layout来修饰,也需要class来妆容

Effect :

这里写图片描述

首先我们要创建一个喜大普奔的Layout

custom_toast_us :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shapo_toast"
    android:gravity="center"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp" >

    <TextView
        android:id="@+id/tv_custom_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"/>

</FrameLayout>

我们在最外层布局的background的属性是一个关键,在Drawable文件夹下创建一个shapo的xml(这里我们的Toast背景颜色就是填充色的颜色,所以想要什么背景颜色就调制什么样子的就好,默认这里是透明的):

shapo_toast :

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

    <!-- 填充的颜色 -->
    <solid android:color="#B0000000" />

    <!-- android:radius 弧形的半径 -->
    <corners android:radius="20dip" />

</shape>

我们自定义的CustomToast :

package com.example.dow.customtoast;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by YongLiu on 2017/6/8.
 */

public class CustomToast {

    private Toast mToast;

    /**
     * 1.连接对应的layout -view
     * 2.设置对象的text用于显示
     * 3.调用原生Toast进行关联与调用
     * */
    public CustomToast(Context context, CharSequence content, int duration){
        View view = LinearLayout.inflate(context, R.layout.custom_toast_us, null);
        TextView mContent = (TextView) view.findViewById(R.id.tv_custom_content);
        mContent.setText(content);
        mToast = new Toast(context);
        mToast.setDuration(duration);
        mToast.setView(view);
    }

    /**
     * 定义us的Toast方法
     * */
    public  static CustomToast makeToast(Context context,CharSequence content,int duration){
        return new CustomToast( context, content, duration);
    }

    /**
     * 重写show方法,因为现在是我们自己的Toast
     * */
    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }

}

无关紧要的 MainActivity:

package com.example.dow.customtoast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView mSystem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView  mSystem =  (TextView) findViewById(R.id.tv_system);
        TextView  mCustom = (TextView) findViewById(R.id.tv_custom);

        /**
         * 系统原生
         * Toast的时间长短,Toast默认有俩种 short为2秒, long3.5左右
         * */
        mSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"系统原生的Toast样式",Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * 自定义Toast
         * Toast的时间如果不想输入类似Toast.LENGTH_SHORT的方法,可以尝试随便输入数字,一般默认2.5,当然如果最后有问题的话就改回来咯
         * */
        mCustom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomToast.makeToast(MainActivity.this,"自定义原生的Toast样式",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值