android自定义Toast

来源:互联网 发布:python黑帽子和灰帽子 编辑:程序博客网 时间:2024/06/06 18:55

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>

0 0
原创粉丝点击