Toast的作用和使用方法

Toasts
Toast API
android Toast大全(五種情形)建立屬于你自己的Toast

toast.PNG

1.作用

Toast 提供有關(guān)操作的簡單反饋,它以小窗口的形式展示。


2.使用方法


2.1 默認方法

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

還可以用如下方法:

Toast.makeText(MainActivity.this, "默認吐司", Toast.LENGTH_SHORT).show();


2.2 自定義顯示位置

主要通過以下兩個 API 實現(xiàn)

setGravity(int gravity, int xOffset, int yOffset);
setMargin(float horizontalMargin, float verticalMargin);

代碼如下:

Toast toast = Toast.makeText(this, "自定義顯示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();


2.3 帶圖片

查看源碼

View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);

可知,Toast 自帶的布局是 transient_notification.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginHorizontal="24dp"
        android:layout_marginVertical="15dp"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/primary_text_default_material_light"
        />

</LinearLayout>

我們可以得到父布局對象,并添加其他視圖。

代碼如下:

Toast toast = Toast.makeText(this, "帶圖片", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout ll = (LinearLayout) toast.getView();
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.btn_circle_pressed);
ll.addView(iv, 0);
toast.show();


2.4 完全自定義視圖

代碼如下:

View layout = LayoutInflater.from(this).inflate(R.layout.layout_toast, null);
TextView tvTop = layout.findViewById(R.id.tv_toast_content);
ImageView ivCenter = layout.findViewById(R.id.iv_toast);
TextView tvBottom = layout.findViewById(R.id.tv_toast);
tvTop.setText("Custom");
ivCenter.setImageResource(R.drawable.btn_circle_pressed);
tvBottom.setText("Custom View");
Toast toast = new Toast(this);
toast.setView(layout);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
toast.show();

或者使用代碼布局也行,代碼如下:

LinearLayout ll = new LinearLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.btn_circle_pressed);
TextView tv = new TextView(this);
tv.setText("自定義視圖");
tv.setTextColor(Color.RED);
tv.setTextSize(20L);
ll.setGravity(Gravity.CENTER_VERTICAL);
ll.addView(iv);
ll.addView(tv);

Toast toast = new Toast(this);
toast.setView(ll);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容