android自定义Toast

本文介绍了如何通过修改源码和创建自定义布局来改变Android中Toast的样式,包括背景颜色、文字大小和阴影效果,提供了具体的布局文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toast是Android中的一个常用组件,用法很简单,主要用于提示信息的显示且 不能获得焦点。

 不能改变他的事件但是我们可以改变他的样式。

首先我们来看一下平时是怎么使用toast的:  Toast.makeText(this,"sehngmsadf",Toast.LENGTH_LONG).show();

那么为什么这样就可以显示一个toast呢?让我们看一下andorid的源码是如何实现的

  public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
        Toast result = new Toast(context);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);    
        result.mNextView = v;
        result.mDuration = duration;
        return result;
    }

可以看到makeText里面首先是载入了一个layout:com.android.internal.R.layout.transient_notification

然后findviewbyid得到一个textview 将我们的文字信息设置上去 

再来看一下这个布局的具体内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">
    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/bright_foreground_dark"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        />
</LinearLayout>

可以看到这只是一个简单的linearlayout布局,布局里面有一个ID= message的textview组件而已。

所以我们可以仿照这个布局自定义一个自己喜欢的,这里我就只是改变了一下文字的大小而已。

toast_self.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/toast_background">
<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_horizontal"
    android:textColor="@color/bright_foreground_dark"
    android:textSize="20dp"
    android:shadowColor="#BB000000"
    android:shadowRadius="5.25"
    />
</LinearLayout>


背景

toast_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <corners android:radius="25dp" />
            <solid android:color="#bb414040" />
            <padding
                android:bottom="10dp"
                android:top="10dp"
                android:left="30dp"
                android:right="30dp" />
        </shape>
    </item>
</layer-list>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值