<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 2.5.7 Toast(吐司)的基本使用 ## 本節引言: > 好的,終于學習完Adapter類相關的一些控件,當然除了講解的那幾個,還有其他很多的 相關的控件,就不慢慢講解了~有需要的自行查閱文檔,查看相關的用法,本節帶來的是: Android用于提示信息的一個控件——Toast(吐司)!Toast是一種很方便的消息提示框,會在 屏幕中顯示一個消息提示框,沒任何按鈕,也不會獲得焦點一段時間過后自動消失! 非常常用!本節我們就來學習Toast的使用! ## 1.直接調用Toast類的makeText()方法創建 這是我們用的最多的一種形式了!比如點擊一個按鈕,然后彈出Toast,用法: **Toast.makeText(MainActivity.this, "提示的內容", Toast.LENGTH_LONG).show();** 第一個是上下文對象!對二個是顯示的內容!第三個是顯示的時間,只有LONG和SHORT兩種 會生效,即時你定義了其他的值,最后調用的還是這兩個! 另外Toast是非常常用的,我們可以把這些公共的部分抽取出來,寫到一個方法里! 需要顯示Toast的時候直接調用這個方法就可以顯示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); //設置顯示位置 TextView v = (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.YELLOW); //設置字體顏色 toast.show(); } ``` 上面這個抽取出來的方法,我們發現我們可以調用setGravity設置Toast顯示的位置以及獲得 通過findViewById(android.R.id.message)獲得顯示的文本,然后進行設置顏色,或者大小等! 這就是第二種通過構造方法來定制Toast! ## 2.通過構造方法來定制Toast: 上面定制了文本,以及顯示位置,下面我們寫兩個簡單的例子: ### 1.定義一個帶有圖片的Toast **效果圖**: ![](http://www.runoob.com/wp-content/uploads/2015/09/99355199.jpg) **關鍵代碼**: ``` private void midToast(String str, int showTime) { Toast toast = Toast.makeText(mContext, str, showTime); toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //設置顯示位置 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); //設置字體顏色 toast.show(); } ``` ### 2.Toast完全自定義 > 如果上面的那種還滿足不了你的話,那么你完全可以自己寫一個Toast的布局,然后顯示出來; 但是時間我們依舊控制不了! **運行效果圖**: ![](http://www.runoob.com/wp-content/uploads/2015/09/50636680.jpg) **關鍵代碼**: ``` 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"> <!-- 設置透明背景色 --> <solid android:color="#BADB66" /> <!-- 設置一個黑色邊框 --> <stroke android:width="1px" android:color="#FFFFFF" /> <!-- 設置四個圓角的半徑 --> <corners android:bottomLeftRadius="50px" android:bottomRightRadius="50px" android:topLeftRadius="50px" android:topRightRadius="50px" /> <!-- 設置一下邊距,讓空間大一點 --> <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> ``` 非常簡單,嘿嘿~ ## 3.示例代碼下載 [ToastDemo.zip](http://www.runoob.com/wp-content/uploads/2015/09/ToastDemo.zip) ## 本節小結: 好的,本節給大家講解了Toast的基本使用,以及如何自定義Toast,非常簡單,大家可以在實際開發中對自己的Toast進行定制~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看