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

问题描述 投票:20回答:8

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

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

我想用另一种不在onCreate()中的方法创建它。

android android-toast
8个回答
58
投票

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

您可以将一个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();

42
投票

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

9
投票

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

4
投票

您可以使用以下代码自定义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();
    }
}

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


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

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

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和多个TextViews放入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>

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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.