直播网站源码Android自定义toast显示效果

编码实现

创新互联公司专业为企业提供定陶网站建设、定陶做网站、定陶网站设计、定陶网站制作等企业网站建设、网页设计与制作、定陶企业网站模板建站服务,十余年定陶做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

**ToastUtils.java**

public class ToastUtils {
    private static Toast mToast;
    public static void showShortText(String text){
        showText(text,Toast.LENGTH_SHORT);
    }
    public static void showLongText(String text){
        showText(text,Toast.LENGTH_LONG);
    }
    public static void showText(String text,int duration){
        if (TextUtils.isEmpty(text)){
            return;
        }
        TextView tvText;
        if (mToast == null){
            mToast = Toast.makeText(MyApplication.getContext(), "", Toast.LENGTH_SHORT);
            final View toastLayout = ((LayoutInflater) MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.toast_layout, null);
            tvText = toastLayout.findViewById(R.id.toast_text);
            mToast.setView(toastLayout);
        }else {
            mToast.cancel();
            mToast = Toast.makeText(MyApplication.getContext(), "", Toast.LENGTH_SHORT);
            final View toastLayout = ((LayoutInflater) MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.toast_layout, null);
            tvText = toastLayout.findViewById(R.id.toast_text);
            mToast.setView(toastLayout);
        }
        mToast.setDuration(duration);
        tvText.setText(text);
        mToast.show();
    }
    
    // 普通方法.....
    public static void showToast(String msg, int duration) {
        
        mToast = Toast.makeText(MyApplication.getContext(), "", duration);
        mToast.setText(msg);
        mToast.show();
    }
}
```
**toast_layout.xml**
```clike
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toast_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_toast">
    <TextView
        android:textSize="13.5sp"
        android:textColor="@color/white"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:id="@+id/toast_text"
        android:layout_gravity="center_horizontal"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

**bg_toast**

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="6dp"/>
    <solid android:color="#cc555555" />
</shape>

使用

ToastUtils.showShortText("1111");

总结

自定义背景效果还是比较简单的,有利于统一机型效果,不是很复杂。主要是从toast源码提供的api我们进行了相应的扩展,容易的实现是建立在对原理的理解与运用上的,多多注重基础,一起前进。

新闻名称:直播网站源码Android自定义toast显示效果
本文网址:https://www.cdcxhl.com/article42/ggoghc.html

成都网站建设公司_创新互联,为您提供网站制作电子商务关键词优化外贸建站网站建设品牌网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设