Android Toast 简单分析
AguedaNcx
8年前
<h3>介绍</h3> <p>A toast provides simple feedback about an operation in a small popup. It only fills the amount of</p> <p>space required for the message and the current activity remains visible and interactive. For</p> <p>example, navigating away from an email before you send it triggers a “Draft saved” toast to let</p> <p>you know that you can continue editing later. Toasts automatically disappear after a timeout.</p> <p>翻译:</p> <p>Toast以一种小弹框的方式来给予用户反馈,它只需要消息可以显示出来的那小部分空间,同时Activity依</p> <p>然可见可交互。例如,当你写邮件的时候退出,会触发“草稿已保存”的Toast来让你知道你以后可以继续</p> <p>编辑这封邮件。Toast会在一段时间后自己消失。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/dc9d3e5395833eab8c48e32dcb4100b1.png"></p> <h3>类结构</h3> <p>Toast的成员</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/dab17b994c1a9cd9b88758bee4334f9c.png"></p> <p>成员解释</p> <table> <thead> <tr> <th>成员</th> <th>含义</th> </tr> </thead> <tbody> <tr> <td>Toast(Context)</td> <td>构造函数</td> </tr> <tr> <td>show()</td> <td>显示Toast</td> </tr> <tr> <td>cancel()</td> <td>取消Toast</td> </tr> <tr> <td>setMargin(float,float)</td> <td>设置横向的margin和纵向的margin</td> </tr> <tr> <td>setGravity(int ,int, int)</td> <td>设置toast的位置重心和相对的XOffset, YOffset</td> </tr> <tr> <td>makeText(Context, CharSequence, int)</td> <td>利用字符串创建Toast</td> </tr> <tr> <td>makeText(Context, int, int)</td> <td>利用字符串资源创建Toast</td> </tr> <tr> <td>setView(View)</td> <td>设置自定义Toast显示的View</td> </tr> <tr> <td>setDuration(int)</td> <td>设置Toast的显示时间</td> </tr> <tr> <td>setText</td> <td>设置Message文本内容</td> </tr> <tr> <td>备注:</td> <td>部分getter方法和setter方法对应,没有写入</td> </tr> </tbody> </table> <h3>实际使用</h3> <p><em>布局文件</em> activity_main.xml</p> <pre> <code class="language-java"><?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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="mraz.com.toastdemo.MainActivity"> <Button android:id="@+id/bt_simple" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:text="Simple Toast" /> <Button android:id="@+id/bt_custom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="@color/colorPrimary" android:text="Custom Toast" /> </LinearLayout></code></pre> <p>自定义的Toast布局</p> <p>custom_toast.xml</p> <pre> <code class="language-java"><?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="40dp" android:background="@drawable/shape_bg" android:orientation="horizontal"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/ic_account_circle_black_24dp" /> <TextView android:layout_width="wrap_content" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center" android:text="This is a Custom Toast !" android:textStyle="italic" /> </LinearLayout></code></pre> <p>代码内容</p> <pre> <code class="language-java">package mraz.com.toastdemo; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Context mContext; Button btSimple, btCustom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; btSimple = (Button) findViewById(R.id.bt_simple); btCustom = (Button) findViewById(R.id.bt_custom); btSimple.setOnClickListener(this); btCustom.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_simple: Toast.makeText(mContext, " This is a simple toast", Toast.LENGTH_SHORT).show(); break; case R.id.bt_custom: LayoutInflater layoutInflater = LayoutInflater.from(mContext); View custToast = layoutInflater.inflate(R.layout.custom_toast, null); Toast toast = new Toast(mContext); toast.setView(custToast); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); break; } } }</code></pre> <h3>实际效果</h3> <p style="text-align:center"><img src="https://simg.open-open.com/show/6e8e7c90fed9a065c8dd2a7f6f339044.gif"></p> <h3>备注</h3> <p>Toast的内容比较简单,但是开发过程中应该会经常用到,说不定偶尔还会碰到显示不出来的情况,不要忘记执行show()方法,当然,还有可能是其他的原因导致你的Toast无法显示~</p> <p> </p> <p>来自:http://blog.csdn.net/poorkick/article/details/51842700</p> <p> </p>