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

时间:2015-07-02 04:27:34

标签: android android-toast

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

Toast.makeText(Logpage.this, "Please Give Feedback...", 3000).show();

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

15 个答案:

答案 0 :(得分:55)

您可以创建如下所示的自定义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();

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

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

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();

答案 1 :(得分:33)

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

这是我发现更改Toast实际图像背景颜色以及文本颜色的一种非常简单的方法,它不需要任何其他布局或任何XML更改:< / p>

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();

答案 2 :(得分:7)

更改默认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();

答案 3 :(得分:4)

创建一个布局文件toast.xml,看看你的toast如何看起来如下:

<?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>

要在java文件中显示toast,请输入以下代码:

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();         
    }

答案 4 :(得分:4)

保持圆角

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

答案 5 :(得分:3)

  

您可以使用以下代码

自定义Android原生吐司
/**
 * 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();
    }
}

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

答案 6 :(得分:3)

在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();

只用这种方式改变吐司面包的颜色。

        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();

答案 7 :(得分:2)

科特林版本:

 val toast = Toast.makeText(this, getString(R.string.back_again), Toast.LENGTH_SHORT)
 val view = toast.view
 view.background.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN)
 toast.show()

答案 8 :(得分:1)

吐司吐司= Toast.makeText(MainActivity.this,R.string.toastMessage,Toast.LENGTH_LONG);                      toast.getView()。setBackgroundColor(Color.parseColor(“#F6AE2D”));                     toast.show();

答案 9 :(得分:0)

添加到@ AndroidKiller的答案,您还可以设置gravity和自定义TextView等其他内容:

Toast toast = Toast.makeText(context, context.getResources().getString(resID), Toast.LENGTH_LONG);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );        
View toastView = li.inflate(R.layout.toast_hint_layout, null);
TextView text = (TextView) toastView.findViewById(R.id.hint_text_tv);
text.setText(resID);
toast.setView(toastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toastView.setBackgroundResource(R.drawable.toast_9_patch);          
toast.show();

注意,您的背景可绘制应为nine-patch PNG

您甚至可以将ImageView和多个TextView放入XML中,如下所示:

<LinearLayout android:id="@+id/layout_root"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
    <ImageView
        android:layout_width="32dp"
        android:layout_height="43dp"
        android:src="@drawable/lightbulb"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/hint_text_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ccc"
            android:textSize="14dp"
            />

        <TextView
            android:id="@+id/hint_text_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="(disable hints in preferences)"
            android:textColor="#555"
            android:textSize="11dp"
            />
    </LinearLayout>
</LinearLayout>

答案 10 :(得分:0)

static void CustomToast(Context context, String text, int duration,
                                    @Nullable Integer backgroundColor,
                                    @Nullable Integer textColor){
        Toast t = Toast.makeText(context,text,duration);
        if (backgroundColor != null)
            t.getView()
                    .setBackgroundTintList(ColorStateList.valueOf(backgroundColor));
        if (textColor != null)
            ((TextView)t.getView().findViewById(android.R.id.message))
                    .setTextColor(textColor);
        t.show();
    }

答案 11 :(得分:0)

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

答案 12 :(得分:0)

我能够通过设置背景滤色器颜色并找到烤面包资源ID并设置文本颜色来做到这一点。

Android.Graphics.Color

    /// <summary> Creates and displays a toast with the given text. </summary>
    public void Show(string message)
    {
        // Create the toast.
        Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);

        // Set the background color.
        Android.Graphics.Color c = new Android.Graphics.Color(122, 193, 66, 255);
        Android.Graphics.ColorMatrixColorFilter CMF = new Android.Graphics.ColorMatrixColorFilter(new float[]
            {
                0,0,0,0,(float)c.R,
                0,0,0,0,(float)c.G,
                0,0,0,0,(float)c.B,
                0,0,0,1,0
            });
        toast.View.Background.SetColorFilter(CMF);

        // Set the text color.
        toast.View.FindViewById<TextView>(Android.Resource.Id.Message).SetTextColor(new Android.Graphics.Color(0, 55, 103, 255));

        // Display the toast.
        toast.Show();
    }

答案 13 :(得分:0)

科林版本:

val toast:Toast = Toast.makeText(this, "Please enter your name !", Toast.LENGTH_SHORT)
val view = toast.view
view?.background?.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
toast.show()

答案 14 :(得分:0)

保持圆形形状的简短Java代码:

Toast toast = Toast.makeText(context, "message", Toast.LENGTH_SHORT);
toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED));
toast.show();