一只大二狗的Android历程--自定义Toast样式

2017年3月14日 10:34 PM
今天看了点自定义Toast的文章,看了篇易懂的,自己改了下,在这里说一下
自定义Toast需要创建工具类和自定义样式(在这里感谢博主DayDayPlayPhone写的博文 《Android Toast的完全自定义与工具类的编写》的对我的启发)
其实就是按照Toast的样式自己写一个方法,然后再调用这个方法
比如说

Toast.makeText(Activity activity,String message,int Duration).show();

我们可以新建一个工具类,然后在类里新建一个静态全局方法customToast();

customToast(Activity activity,String message,int Duration){
    。。。。。。
}

好了废话不多说,开始上源代码:
首先新建一个布局样式文件 toast.xml

<?xml version="1.0" encoding="utf-8"?>
<!--引用样式文件shape.xml-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"      android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape">
    <ImageView
        android:id="@+id/toast_iv"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/coffe"/>
    <TextView
        android:gravity="center"
        android:id="@+id/toast_tv"
        android:textColor="@android:color/white"
        android:layout_marginLeft="0dp"
        android:layout_below="@id/toast_iv"
        android:layout_toRightOf="@id/toast_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="Text"/>
</RelativeLayout>

在Drawable里新建样式文件shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#6495ED"/>
    <corners android:radius="20dip"/>
    <padding
        android:right="10dp"
        android:bottom="5dp"
        />
</shape>

然后新建类CT(注:CustomToast简写),类里有方法customToast()

import android.app.Activity;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by li124 on 2017/3/14.
 */

public class CT {
    //定义全局静态方法customToast
    //其中的参数对应Toast.makeText里的Activity ,message,Duration
    public static void customToast(Activity activity,String message,int Duration){
        //使用布局加载器加载自定义的布局
        View view= LayoutInflater.from(activity).inflate(R.layout.toast,null);
        //通过获取的文字设置到布局上
        TextView title=(TextView)view.findViewById(R.id.toast_tv);
        title.setText(message);
        //新建一个Toast方法
        Toast toast=new Toast(activity);
        //设置Toast的在屏幕的位置,第三个参数是x值,第四个参数是y值,距离底部的高度
        toast.setGravity(Gravity.CENTER|Gravity.BOTTOM,0,500);
        //设置显示的时长
        toast.setDuration(Duration);
        toast.setView(view);
        toast.show();
    }
}

然后在MainActivity里添加Button的点击事件,点击事件里引用方法

    public void show(View view){
        CT.customToast(this,"Press again", 0);
    }

对了,我好像忘了main_layout.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"
    tools:context="com.eee.custom_toast.MainActivity">

    <Button
        android:onClick="show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="SHOW"/>

</RelativeLayout>

注:效果类似于QQ聊天时的气泡样式,我因为美工渣做不出透明的图片所以引用了网上别的博文的素材,版权原因我就不在这里演示了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值