最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義Toast樣式實現(xiàn)方法詳解

 更新時間:2023年01月17日 08:43:50   作者:FranzLiszt1847  
這篇文章主要介紹了Android自定義Toast樣式,Toast是一種很方便的消息提示框,會在 屏幕中顯示一個消息提示框,沒任何按鈕,也不會獲得焦點一段時間過后自動消失!非常常用!本文就來通過一個例子把Toast的使用講透

原生Toast樣式

自定義Toast樣式

創(chuàng)建樣式

所謂自定義一個Toast就是建立一個布局文件,然后使用一個view容器承載,然后顯示出來。Toast畢竟只是起到一個提示作用,不必要放太多控件去填充,反而會顯得內容很擁擠,一般一個標題,一張圖片,一段簡短文字即可。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/white_radius_10">
    <LinearLayout
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_gravity="center">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/icon_error" />
        <TextView
            android:id="@+id/ErrorTips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="密碼錯誤,請重試!"
            android:textColor="#099A9F"
            android:textSize="15sp"
            android:ellipsize="end"/>
    </LinearLayout>
</FrameLayout>

封裝

因為我們在開發(fā)中需要頻繁使用Toast,所以使用一個類將其封裝起來,復用其功能,增強代碼可讀性,可行性。根據(jù)自己的開發(fā)需求,可以向外暴露修改內容,圖片,顏色,文字大小等方法,由于Toast內附控件較少,就可以直接將其控件ID和布局文件定義為常量引用。

public class ToastFormat {
    private Context context;
    private TextView tipsText;
    private Toast toast = null;
    private static final int ContentID = R.id.ErrorTips;
    private static final int LayoutID = R.layout.passworderror;
    public ToastFormat(Context context){
        this.context = context;
    }
    public void InitToast(){
        if (toast == null) {
            toast = new Toast(context);
            View view = LayoutInflater.from(context).inflate(LayoutID, null, false);
            tipsText = view.findViewById(ContentID);
            toast.setView(view);
        }
    }
    public void setGravity(int gravity){
        toast.setGravity(gravity, 0, 0);
    }
    public void setText(String tips){
        tipsText.setText(tips);
    }
    public void show(){
        toast.show();
    }
    public void setShowTime(int time){
        toast.setDuration(time);
    }
    public void setTextColor(int color){
        tipsText.setTextColor(context.getResources().getColor(color));
    }
    public void setTextSize(float size){
        tipsText.setTextSize(size);
    }
}

引用

定義對象

private ToastFormat format;

初始化

format = new ToastFormat(MainActivity.this);
format.InitToast();

設置顯示文字內容

format.setText("自定義Toast");

最后顯示 Toast

format.show();

到此這篇關于Android自定義Toast樣式實現(xiàn)方法詳解的文章就介紹到這了,更多相關Android自定義Toast內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

贵定县| 西盟| 沈阳市| 永康市| 重庆市| 大兴区| 万载县| 太湖县| 临江市| 济南市| 梨树县| 视频| 东辽县| 娄底市| 剑阁县| 维西| 忻城县| 明水县| 特克斯县| 宣汉县| 资阳市| 遂平县| 峡江县| 亳州市| 吉林省| 射洪县| 密山市| 防城港市| 峨眉山市| 彭山县| 宁德市| 凌源市| 镇安县| 永川市| 莎车县| 洛阳市| 浮山县| 葫芦岛市| 潞西市| 鸡西市| 油尖旺区|