【问题标题】:Issue with custom Toast with color带有颜色的自定义吐司问题
【发布时间】:2020-07-20 17:45:36
【问题描述】:

我尝试使用背景“橙色”制作自定义吐司,但是,在尝试中我能够得到它,但该字段也变为白色并且文本不可见,我已附加图像

可以看到文字是不可见的。

这是我的代码:

            String g= "+";
            Toast toast = Toast.makeText(getActivity(), "Click and hold on '"+g+"' icon", 
             Toast.LENGTH_SHORT);

            View view= toast.getView();
            view.setBackgroundColor(Color.parseColor("#FF791B"));

            View t = toast.getView().findViewById(android.R.id.message);
            t.setBackgroundColor(Color.parseColor("#FFFFFF"));
            toast.show();

我只希望文本是白色的。 是我在代码中遗漏的东西,我无法正确处理。 另外,有没有办法让“+”号加粗g

【问题讨论】:

    标签: android toast android-toast


    【解决方案1】:

    只需更改这一行

      t.setBackgroundColor(Color.parseColor("#FFFFFF"));
    

    收件人

       t.setTextColor(Color.parseColor("#FFFFFF"));
    

    【讨论】:

    • 这里好像没有.setTextColor 方法
    【解决方案2】:

    我在这里对模式进行了一些更改:

    TextView t = toast.getView().findViewById(android.R.id.message);
                t.setTextColor(Color.WHITE);
    

    我已将其保存为 textView 并且我能够获得 setTextColor 方法并且我能够更改颜色

    【讨论】:

      【解决方案3】:

      试试这个 - customttoast.xml 是您的自定义 toast xml,而 custom_toast_layout 可以是您的活动布局 xml

              //Creating the LayoutInflater instance  
              LayoutInflater li = getLayoutInflater();  
              //Getting the View object as defined in the customtoast.xml file  
              View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));  
        
              //Creating the Toast object  
              Toast toast = new Toast(getApplicationContext());  
              toast.setDuration(Toast.LENGTH_SHORT);  
              toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);  
              toast.setView(layout);//setting the view of custom toast layout  
              toast.show();  
      

      【讨论】:

      • 这个变得冗长,有更多的代码。但是这种方式确实有效,但是对于基于应用程序的服务器来说,更少的代码更有效。
      【解决方案4】:
      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多