当前位置: 首页 > 知识库问答 >
问题:

自定义吐司充气-Android

柴寂离
2023-03-14
public class MyToast extends Toast {

    public static final int RED = 0;
    public static final int BLUE = 1;

    public MyToast(Context context, String data, int color) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.my_toast, null);
        setView(layout);

        TextView tvToast = (TextView) layout.findViewById(R.id.tvToast);
        tvToast.setText(data);

        if(color == RED) {
            tvToast.setBackgroundResource(R.drawable.red_background);
            tvToast.setTextColor(context.getResources().getColor(R.color.red));
        } else {
            tvToast.setBackgroundResource(R.drawable.blue_background);
            tvToast.setTextColor(Color.WHITE);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tvToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

在mainactivity中使用MyToast:

new MyToast(context, getResources().getString(
                            R.string.uspesna_najava), MyToast.BLUE).show();

我收到这个警告:

避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)

View layout = inflater.inflate(R.layout.my_toast, null);
View layout = inflater.inflate(R.layout.my_toast, parent, false);
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    View layout = inflater.inflate(R.layout.my_toast, (ViewGroup) findViewById(R.id.toast_layout_root));

未为MyToast类型定义findViewById(int)方法

共有1个答案

陆飞鸿
2023-03-14

当然,findViewById方法是一种活动方法。

因此,示例代码位于Activity类中,只调用findViewById不会造成任何问题。

因此,您的代码在MyToast类中,您应该为活动实例调用该方法:

((Activity) context).findViewById(R.id.toast_layout_root)
 类似资料:
  • 我正在尝试使用自定义toast和和。当我触摸任何地方(点击按钮,触摸布局……)时,我希望我的吐司消失,但它没有。 我读取了文件,并尝试在调用新Toast之前使用方法,但这并没有解决任何问题。有人能给我一个解决办法吗?

  • 我有这个祝酒词要出现,一切都很好,除了音频。我试图将toast.mp3放在不同的位置,如项目主文件夹、资产等,甚至使用了d:/myprojectpath/Assets/toast.mp3这样的路径,但通知仍然是静默的。我是个初学者,所以也许我错过了什么...当然,我也在寻找解决方案,但没有任何帮助。下面是我的代码:

  • 问题内容: 我有一个运行远程服务然后退出的android活动。该服务本身在设备节点上进行轮询并检查更改,我想使用Toast来提醒用户,但是我没有让它起作用。Toast没有显示,过一会儿,Android喊我的应用程序没有响应。顺便说一句,我不想​​再次开始活动并从那里显示吐司,我只是希望它在显示给用户的当前屏幕上弹出。 服务代码如下: 问题答案: 您无法通过服务呼叫Toast消息。除了UI线程之外,

  • 函数功能:在屏幕底部以悬浮层形式显示字符串信息 函数方法 toast(text,time) 参数 类型 必填 说明 text string 是 提示信息,将在设备屏幕上以 HUD 形式显示 time number 否 不写默认 0 - 短时间显示,非 0 - 稍长时间显示 函数示例 toast("欢迎使用积木教程!",2); mSleep(3000);-- 建议 toast 函数后面添加 3 秒

  • 我正在从Ajax JSON创建一个DataTable。 将创建DataTables,但它显示一个错误: DataTables警告:表ID=CHANGETABLE-为行0,列0请求未知参数“0”。有关此错误的详细信息,请参阅http://datatables.net/TN/4 我的JSON看起来如下所示 在我的Java控制器中创建了这样的JSON对象: 我不知道如何将此信息与 属性一起使用,以使其适

  • 我有一个自定义的吐司diplaying图像和一些文本。这个祝酒词足够大,几乎可以覆盖半个屏幕。它的持续时间为LENGTH_LONG,因为它包含很多信息。