如何更改android中的默认吐司消息颜色和背景颜色?

Sar*_*nya 29 android android-toast

我想创建一个背景颜色为白色且消息颜色为黑色的Toast消息.我的祝酒词是:

Toast.makeText(Logpage.this, "Please Give Feedback...", 3000).show();
Run Code Online (Sandbox Code Playgroud)

我想用另一种方法创建它而不是onCreate().

And*_*ler 65

您可以创建如下的自定义Toast消息:

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.your_custom_layout, null);
toast.setView(view);
toast.show();
Run Code Online (Sandbox Code Playgroud)

您可以将一个textview放在布局文件中,并根据需要提供背景和文本颜色.

您还可以执行以下操作,不需要额外的自定义布局文件:

Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_background);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
toast.show();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这应该是公认的答案。 (3认同)
  • Google 从 Android 11 开始弃用它,现在我们只能使用原始 Toast! (2认同)

Mat*_*ing 51

更改Toast颜色,无需任何额外的布局,2018年

这是一个非常简单的方法,我发现改变Toast的实际图像背景的颜色以及文本颜色,它不需要任何额外的布局或任何XML更改:

Toast toast = Toast.makeText(context, message, duration);
View view = toast.getView();

//Gets the actual oval background of the Toast then sets the colour filter
view.getBackground().setColorFilter(YOUR_BACKGROUND_COLOUR, PorterDuff.Mode.SRC_IN);

//Gets the TextView from the Toast so it can be editted
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(YOUR_TEXT_COLOUR);

toast.show();
Run Code Online (Sandbox Code Playgroud)

  • 完美的我的要求. (4认同)
  • 非常简单的解决方案,效果很好,谢谢。 (2认同)

Yug*_*esh 11

更改默认Toast文本颜色和背景颜色尝试像这样.

 Toast toast = Toast.makeText(MainActivity.this, "Please Give Feedback...", Toast.LENGTH_LONG);
 View view = toast.getView();

 //To change the Background of Toast
 view.setBackgroundColor(Color.TRANSPARENT);
 TextView text = (TextView) view.findViewById(android.R.id.message);

 //Shadow of the Of the Text Color
 text.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
 text.setTextColor(Color.BLACK);
 text.setTextSize(Integer.valueOf(getResources().getString(R.string.text_size)));
 toast.show();
Run Code Online (Sandbox Code Playgroud)


小智 10

用这种方式

Toast toast = Toast.makeText(MainActivity.this, R.string.toastMessage, Toast.LENGTH_LONG);   
toast.getView().setBackgroundColor(Color.parseColor("#F6AE2D"));
toast.show();
Run Code Online (Sandbox Code Playgroud)

  • 更改背景颜色和文本颜色后,吐司形状变为矩形。为什么会出现这种情况?为什么改变颜色后吐司默认的椭圆形状没有保留? (3认同)
  • 改变颜色后,吐司就变成了乡绅。为什么? (2认同)

Ano*_*p M 7

您可以使用以下代码来自定义android native toast

/**
 * ShowToast
 */
public class ShowToast {
    public ShowToast(Context context, String info) {
        Toast toast = Toast.makeText(context, Html.fromHtml("<font color='#e3f2fd' ><b>" + info + "</b></font>"), Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要更改背景,则必须在吐司中使用自定义布局

  • 这是最好的解决方案 (3认同)

mol*_*kin 6

保持圆角

val toast = Toast.makeText(context, "Text", Toast.LENGTH_SHORT)
toast.view.background.setTintList(ContextCompat.getColorStateList(context, android.R.color.darker_gray))
toast.show()
Run Code Online (Sandbox Code Playgroud)


小智 5

创建一个布局文件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:background="@android:color/background_dark">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a custom toast."
        android:textColor="@android:color/white"
        android:layout_gravity="center_vertical" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

要在Java文件中显示吐司,请输入以下代码:

public void showCustomAlert()
    {         
        Context context = getApplicationContext();
        // Create layout inflator object to inflate toast.xml file
        LayoutInflater inflater = getLayoutInflater();

        // Call toast.xml file for toast layout 
        View toast = inflater.inflate(R.layout.toast, null);

        Toast toast = new Toast(context);

        // Set layout to toast 
        toast.setView(toast);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
                0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();         
    }
Run Code Online (Sandbox Code Playgroud)

  • 为什么 2 个变量具有相同的 anem `toast` (2认同)

sat*_*ker 5

Toast toast=   Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want 
toast.show();
Run Code Online (Sandbox Code Playgroud)


小智 5

在 JAVA 中更改默认的 toast 消息颜色和背景颜色。您可以通过这种方式更改吐司消息颜色和背景颜色。

        Toast toast=Toast.makeText(MainActivity.this,"Signin button is clicked.",Toast.LENGTH_SHORT);
        View view =toast.getView();
        view.setBackgroundColor(Color.GREEN);
        TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
        toastMessage.setTextColor(Color.RED);
        toast.show();
Run Code Online (Sandbox Code Playgroud)

只需通过这种方式更改吐司文本颜色..

        Toast toast = Toast.makeText(getApplicationContext(), "Signup button is clicked.",Toast.LENGTH_SHORT);

        TextView toastMessage=(TextView) toast.getView().findViewById(android.R.id.message);
        toastMessage.setTextColor(Color.BLUE);
        toast.show();
Run Code Online (Sandbox Code Playgroud)