W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
好的,終于學(xué)習(xí)完Adapter類相關(guān)的一些控件,當(dāng)然除了講解的那幾個,還有其他很多的 相關(guān)的控件,就不慢慢講解了~有需要的自行查閱文檔,查看相關(guān)的用法,本節(jié)帶來的是: Android用于提示信息的一個控件——Toast(吐司)!Toast是一種很方便的消息提示框,會在 屏幕中顯示一個消息提示框,沒任何按鈕,也不會獲得焦點一段時間過后自動消失! 非常常用!本節(jié)我們就來學(xué)習(xí)Toast的使用!
這是我們用的最多的一種形式了!比如點擊一個按鈕,然后彈出Toast,用法: Toast.makeText(MainActivity.this, "提示的內(nèi)容", Toast.LENGTH_LONG).show(); 第一個是上下文對象!對二個是顯示的內(nèi)容!第三個是顯示的時間,只有LONG和SHORT兩種 會生效,即時你定義了其他的值,最后調(diào)用的還是這兩個!
另外Toast是非常常用的,我們可以把這些公共的部分抽取出來,寫到一個方法里! 需要顯示Toast的時候直接調(diào)用這個方法就可以顯示Toast,這樣方便很多! 示例如下:
void midToast(String str, int showTime)
{
Toast toast = Toast.makeText(global_context, str, showTime);
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0); //設(shè)置顯示位置
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.YELLOW); //設(shè)置字體顏色
toast.show();
}
上面這個抽取出來的方法,我們發(fā)現(xiàn)我們可以調(diào)用setGravity設(shè)置Toast顯示的位置以及獲得 通過findViewById(android.R.id.message)獲得顯示的文本,然后進(jìn)行設(shè)置顏色,或者大小等! 這就是第二種通過構(gòu)造方法來定制Toast!
上面定制了文本,以及顯示位置,下面我們寫兩個簡單的例子:
效果圖:
關(guān)鍵代碼:
private void midToast(String str, int showTime)
{
Toast toast = Toast.makeText(mContext, str, showTime);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //設(shè)置顯示位置
LinearLayout layout = (LinearLayout) toast.getView();
layout.setBackgroundColor(Color.BLUE);
ImageView image = new ImageView(this);
image.setImageResource(R.mipmap.ic_icon_qitao);
layout.addView(image, 0);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.YELLOW); //設(shè)置字體顏色
toast.show();
}
如果上面的那種還滿足不了你的話,那么你完全可以自己寫一個Toast的布局,然后顯示出來; 但是時間我們依舊控制不了!
運行效果圖:
關(guān)鍵代碼:
private void midToast(String str, int showTime)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.view_toast_custom,
(ViewGroup) findViewById(R.id.lly_toast));
ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
tv_msg.setText(str);
Toast toast = new Toast(mContext);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
還有自定義Toast的布局以及圓角背景:
圓角背景:bg_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設(shè)置透明背景色 -->
<solid android:color="#BADB66" />
<!-- 設(shè)置一個黑色邊框 -->
<stroke
android:width="1px"
android:color="#FFFFFF" />
<!-- 設(shè)置四個圓角的半徑 -->
<corners
android:bottomLeftRadius="50px"
android:bottomRightRadius="50px"
android:topLeftRadius="50px"
android:topRightRadius="50px" />
<!-- 設(shè)置一下邊距,讓空間大一點 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
布局文件:view_toast_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lly_toast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_toast"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_logo"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:src="@mipmap/iv_lol_icon1" />
<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20sp" />
</LinearLayout>
非常簡單,嘿嘿~
好的,本節(jié)給大家講解了Toast的基本使用,以及如何自定義Toast,非常簡單,大家可以在實際開發(fā)中對自己的Toast進(jìn)行定制~
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: