Android中自定义Toast文字大小

在Android开发中,Toast是一种轻量级的提示框,用于在屏幕上显示临时消息。一般情况下,Toast显示的文字大小是固定的,无法直接改变。但是,我们可以通过一些方法来实现在Toast中显示不同大小的文字。

方法一: 使用自定义布局

创建custom_toast.xml布局文件,如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="vertical"
    android:padding="16dp">
<!--    android:background="@drawable/toast_background"-->
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="18sp" />

</LinearLayout>

  java代码:

public static void showToastWithCustomSize(Context context, String message, int textSize) {
        // 加载自定义Toast布局
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(com.xzh.cssmartandroid.R.layout.custom_toast, null);

        // 设置TextView的字体大小
        TextView textView = layout.findViewById(com.xzh.cssmartandroid.R.id.text_view);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textView.setText(message);

        // 创建并显示Toast
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

  方法二:使用SpannableString

 public static void showToast(Context context, String message, int textSize){
        SpannableString spannableString = new SpannableString(message);
        spannableString.setSpan(new AbsoluteSizeSpan(textSize,true),0,spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        Toast.makeText(context,spannableString,Toast.LENGTH_SHORT).show();
    }

  以上两种方法就可以实现改变Toast文字大小了

封装

/**
 * 自定义Toast文字大小
 */
public class ToastUtils {
    /**
     * 方法1
     * @param context
     * @param message
     * @param textSize
     */
    public static void showToastWithCustomSize(Context context, String message, int textSize) {
        // 加载自定义Toast布局
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(com.xzh.cssmartandroid.R.layout.custom_toast, null);

        // 设置TextView的字体大小
        TextView textView = layout.findViewById(com.xzh.cssmartandroid.R.id.text_view);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textView.setText(message);

        // 创建并显示Toast
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

    /**
     * 方法2
     * @param context
     * @param message
     * @param textSize
     */
    public static void showToast(Context context, String message, int textSize){
        SpannableString spannableString = new SpannableString(message);
        spannableString.setSpan(new AbsoluteSizeSpan(textSize,true),0,spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        Toast.makeText(context,spannableString,Toast.LENGTH_SHORT).show();
    }
}

 

posted on 2024-06-12 17:42  巫山老妖  阅读(24)  评论(0编辑  收藏  举报