日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android custom toast,Android自定义Toast

發布時間:2023/12/2 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android custom toast,Android自定义Toast 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

核心代碼:

package com.huatec.myapplication;

import android.content.Context;

import android.graphics.Bitmap;

import android.support.annotation.ColorInt;

import android.support.annotation.DrawableRes;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

/**

* 自定義吐司

*/

public final class MyToast {

public final Toast toast;//Toast對象

public final View view;//Toast的UI效果

public final ImageView icon;//圖標

public final TextView message;//內容

public MyToast(Context context) {

toast = new Toast(context);

view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);

icon = view.findViewById(R.id.toast_icon);

message = view.findViewById(R.id.toast_message);

}

/**

* 顯示

*/

public void show() {

this.toast.show();

}

public static class Builder {

private Bitmap icon;//圖標圖片

// private int iconID = R.mipmap.ic_launcher;//圖標資源ID

private int iconID;//

private String message;//內容

private int backgroundColor = 0x56000000;//背景顏色

private Context mContext;//上下文

private int duration = Toast.LENGTH_SHORT;//設置時間

private MyToast mine;

private int gravity = Gravity.NO_GRAVITY;//設置位置

private int offsetX = 0;//設置偏移度X

private int offsetY = 0;//設置偏移度Y

private boolean isShowIcon;//是否顯示圖標

public Builder(Context context) {

this.mContext = context;

}

/**

* 設置ICON

*

* @param bitmap

* @return

*/

public Builder setIcon(Bitmap bitmap) {

this.icon = bitmap;

return this;

}

public Builder setIcon(@DrawableRes int resId) {

this.iconID = resId;

return this;

}

public Builder showIcon(boolean showIcon) {

this.isShowIcon = showIcon;

return this;

}

/**

* 設置內容

*/

public Builder setMessage(String hintMessage) {

this.message = hintMessage;

return this;

}

/**

* 設置吐司時長

*/

public Builder setDuration(int type) {

this.duration = type;

return this;

}

/**

* 設置背景

*/

public Builder setBackgroundColor(@ColorInt int color) {

this.backgroundColor = color;

return this;

}

/**

* 設置位置

*/

public Builder setGravity(int gravity) {

this.gravity = gravity;

return this;

}

/**

* 偏移量

*/

public Builder setOffsetX(int x) {

this.offsetX = x;

return this;

}

public Builder setOffsetY(int y) {

this.offsetY = y;

return this;

}

/**

* 創建MyToast對象

*/

public MyToast build() {

if (null == mine) {

mine = new MyToast(mContext);//創建對象

}

if (isShowIcon) {

//隱藏圖標

mine.icon.setVisibility(View.VISIBLE);

if (null != icon) {//判斷是否顯示圖標

mine.icon.setImageBitmap(icon);//設置圖片

} else if (iconID != 0) {

//設置圖片

mine.icon.setBackgroundResource(iconID);

}

}

if (!message.isEmpty()) {//判斷內容是否為空

mine.message.setText(message);

} else {

mine.message.setText("");

}

//設置背景

mine.view.setBackground(new BackgroundDrawable(backgroundColor, mContext));

//mine.message.setTextColor(Color.BLACK);//設置字體顏色

mine.toast.setDuration(duration);//設置時長

mine.toast.setView(mine.view);//添加自定義效果

mine.toast.setGravity(gravity, offsetX, offsetY);//設置偏移量

return mine;

}

}

}

toast_layout.xml布局文件:

android:orientation="horizontal" android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="10dp"

android:paddingRight="10dp"

android:paddingTop="5dp"

android:paddingBottom="5dp">

android:id="@+id/toast_icon"

android:visibility="gone"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/toast_message"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="5dp"

android:textColor="#ffffff"

android:textSize="16sp"

android:layout_gravity="center_vertical"/>

BackgroundDrawable.java背景:

package com.huatec.myapplication;

import android.annotation.TargetApi;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.ColorFilter;

import android.graphics.Paint;

import android.graphics.PixelFormat;

import android.graphics.drawable.Drawable;

import android.os.Build;

import android.support.annotation.ColorInt;

import android.util.TypedValue;

public class BackgroundDrawable extends Drawable{

private Paint paint;

private Context mContext;

public BackgroundDrawable(@ColorInt int color,Context context) {

mContext = context.getApplicationContext();

paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setDither(true);

paint.setStyle(Paint.Style.FILL);

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

@Override

public void draw(Canvas canvas) {

int width = canvas.getWidth();

int height = canvas.getHeight();

canvas.drawRoundRect(0,0,width,height,dp2px(20),dp2px(20),paint);

}

@Override

public void setAlpha(int alpha) {

paint.setAlpha(alpha);

}

@Override

public void setColorFilter(ColorFilter colorFilter) {

paint.setColorFilter(colorFilter);

}

@Override

public int getOpacity() {

return PixelFormat.TRANSLUCENT;

}

private int dp2px(int values){

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,values,

mContext.getResources().getDisplayMetrics());

}

}

使用Toast:

package com.huatec.myapplication;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.Gravity;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button hintToast1;

private Button hintToast2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

hintToast1 = findViewById(R.id.hint_toast);

hintToast2 = findViewById(R.id.hint_toast2);

//按鈕1

hintToast1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new MyToast.Builder(MainActivity.this)

.setMessage("你好嗎?")//設置提示文字

.setBackgroundColor(0x88ff4587)//設置背景顏色

.setGravity(Gravity.CENTER)//設置吐司位置

.build()//創建吐司

.show();

}

});

//按鈕2

hintToast2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new MyToast.Builder(MainActivity.this)

.setMessage("你好嗎?")

.setBackgroundColor(0x88ff4587)

.setGravity(Gravity.CENTER)

.setIcon(R.mipmap.ic_launcher)//設置圖標

.showIcon(true)//是否顯示圖標

.build()

.show();

}

});

}

}

效果圖:

2018-05-20 18_42_30.gif

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的android custom toast,Android自定义Toast的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。