<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Android帶圖片的Toast(自定義Toast)

      使用Android默認的Toast

      Toast簡介:

      Toast是一個簡單的消息顯示框,能夠短暫的出現在屏幕的某個位置,顯示提示消息。

      默認的位置是屏幕的下方正中,一般Toast的使用如下:

       Toast.makeText(this,"1222222",Toast.LENGTH_SHORT).show();

      Toast是static修飾的靜態類,意味著可以直接使用,所以可以不用創建對象直接調用makeText方法,

      該方法需要傳入三個參數:

         /**
           * Make a standard toast that just contains a text view.
           *
           * @param context  The context to use.  Usually your {@link android.app.Application}
           *                 or {@link android.app.Activity} object.
           * @param text     The text to show.  Can be formatted text.
           * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or
           *                 {@link #LENGTH_LONG}
           *
           */

      第一個參賽數當前context,第二個是需要顯示的文本內容,第三個參數是顯示時間

      但這里的顯示時間只有兩種,一個是 Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG. 顧名思義,后者比前者要長一點。

      自定義Toast

      自定義圖片

      今天看到某音樂播放軟件有個收藏功能會彈出類似效果的Toast

      上面一顆紅??,下面顯示文本內容, 那么這個效果如何實現呢?

      在打開Toast 源碼可以看到一個方法setView

         /**
           * Set the view to show.
           * @see #getView
           */
          public void setView(View view) {
              mNextView = view;
          }

      想必可以通過該方法添加圖片和文本

      那接下來就可以嘗試自定義一個布局文件,并把該布局通過setView的方式添加到Toast里面

      布局文件為線型布局,內容如下,添加一個現形布局,在該線型布局中添加一個ImageView和一個TextView

      該布局文件名為toast_view.xml,設置orientation為vertical為垂直排列,并將準備好的心型圖片設置為ImageView的背景

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@android:color/black"
              android:gravity="center"
              android:minWidth="100dp"
              android:orientation="vertical">
              <!--android:background="@drawable/toast_bg"-->
              <ImageView
                  android:id="@+id/toast_image"
                  android:layout_width="30dp"
                  android:layout_height="30dp"
                  android:layout_gravity="center"
                  android:layout_margin="2dp"
                  android:background="@drawable/redheart" />
      
              <TextView
                  android:id="@+id/toast_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_margin="2dp"
                  android:gravity="center"
                  android:text=""
                  android:textColor="#ffffff"
                  android:textSize="15dp" />
          </LinearLayout>
      
      </LinearLayout>

       

      結下來創建一個ToastView Class,把該布局文件關聯起來

         /**
           * 
           * @param context
           * @param text
           */
          public ToastView(Context context, String text) {
              LayoutInflater inflater = LayoutInflater.from(context);
              View view = inflater.inflate(R.layout.toast_view, null);
              TextView t = (TextView) view.findViewById(R.id.toast_text);
              t.setText(text);
              if (toast != null) {
                  toast.cancel();
              }
              toast = new Toast(context);
              toast.setDuration(Toast.LENGTH_SHORT);
              toast.setView(view);
          }

      通過setText方法把要顯示的文本顯示出來

      當然還可以進一步優化,把ImageView的背景替換掉

       public ToastView(Context context, String text) {
              LayoutInflater inflater = LayoutInflater.from(context);
              View view = inflater.inflate(R.layout.toast_view, null);
              ImageView imageView=(ImageView)view.findViewById(R.id.toast_image);
              imageView.setBackgroundResource(R.mipmap.ic_launcher);
              TextView t = (TextView) view.findViewById(R.id.toast_text);
              t.setText(text);
              if (toast != null) {
                  toast.cancel();
              }
              toast = new Toast(context);
              toast.setDuration(Toast.LENGTH_SHORT);
              toast.setView(view);
          }

      通過這個方法,先獲取到Layout然后通過findViewById獲取到子控件進行設置

      然而這樣的效果依然不是我們想要的,顯示出來并不是帶圓角的

      這個時候就需要添加一個shape布局

      設置圓角,并把該shape添加到LinearLayout的背景

      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <solid android:color="#c83e3e3e" />
      
          <!--radius shape-->
          <corners
              android:bottomLeftRadius="10dp"
              android:bottomRightRadius="10dp"
              android:radius="8dp"
              android:topLeftRadius="10dp"
              android:topRightRadius="10dp" />
      </shape>
         

       

       

      自定義位置

      那么如何自定義顯示位置?

      通過查看Toast的源碼可以看到一個setGravity的方法,是專門用來設置Toast的位置

         /**
           * Set the location at which the notification should appear on the screen.
           * @see android.view.Gravity
           * @see #getGravity
           */
          public void setGravity(int gravity, int xOffset, int yOffset) {
              mTN.mGravity = gravity;
              mTN.mX = xOffset;
              mTN.mY = yOffset;
          }

      該方法有三個參賽,第一個是整形類型的gravity,該參數設置具體的位置,可以參考Gravity類

      一般常用的有:

      Gravity.CENTER
      Gravity.LEFT
      Gravity.RIGHT
      Gravity.TOP
      Gravity.BOTTOM

      顧名思義,第二個和第三個參數是偏移量,針對第一個參數的偏移量

      所以,如果設置Toast在屏幕正當中,只需要這樣

      toast.setGravity(Gravity.CENTER, 0, 0);

       

      自定義Toast的顯示時間

      未完待續。。。。。。

      posted @ 2016-04-26 22:59  to be crazy  閱讀(9895)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产精品不卡一区二区在线| 欧美交a欧美精品喷水| 欧美丰满熟妇xxxx性ppx人交| 国产欧美久久一区二区三区| 久久精品国产91精品亚洲| 亚洲深夜精品在线观看| 日韩精品国产另类专区| 在线播放国产精品三级网| 亚洲精品一区二区麻豆| 久久视频在线视频| 亚洲悠悠色综合中文字幕| 四虎成人精品永久免费av| 国产高清精品在线91| 久热这里有精品视频在线| 羞羞影院午夜男女爽爽免费视频| 国产成人午夜在线视频极速观看| 亚洲精品麻豆一二三区| 国产精品视频一区二区不卡| 亚洲欧美色综合影院| 夜夜躁狠狠躁日日躁视频| 无码人妻精品一区二区三区下载| A级毛片100部免费看| 国产精品SM捆绑调教视频| 亚洲 日本 欧洲 欧美 视频| 日韩一区二区三区水蜜桃| 中文人妻AV大区中文不卡| 亚洲日本VA中文字幕在线| 国产精品无码a∨麻豆| 精品少妇后入一区二区三区| 亚洲VA久久久噜噜噜久久无码| 无码一区中文字幕| 久热这里只有精品在线观看| 亚洲人成网站观看在线观看| 精品久久精品久久精品九九| 精品国产亚洲一区二区三区| 蜜臀98精品国产免费观看 | 国产日韩精品视频无码| 亚洲综合国产精品第一页| 久久久久人妻一区精品色| 久久精品波多野结衣| 日韩精品无码一区二区视频|