我想创建一个白色背景、黑色文字的提示信息。我的提示信息是:
Toast.makeText(Logpage.this, "Please Give Feedback...", 3000).show();
我希望用另一种方法创建它,而不是在onCreate()
中创建。
我想创建一个白色背景、黑色文字的提示信息。我的提示信息是:
Toast.makeText(Logpage.this, "Please Give Feedback...", 3000).show();
我希望用另一种方法创建它,而不是在onCreate()
中创建。
您可以按以下方式创建自定义的提示消息:
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"));*/
text.setTextColor(Color.parseColor("#000000"));
toast.show();
不需要额外布局即可更改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();
为保护用户和维护良好的用户体验,Android 11 在阻止后台应用发送自定义 Toast 上下功夫了。因此,如果一个应用程序是针对 Android 11 的,则系统会阻止该应用程序从后台发送包含自定义视图的 Toast 消息,以确保安全。
在 Android R 中新增了 addCallback() 方法,可以用来在 toast(文本或自定义)出现或消失时进行通知。
您可以使用以下代码自定义 Android 原生 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();
}
}
如果你想要改变背景,你必须在提示中使用自定义布局。
要更改默认的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();
使用这种方式
Toast toast = Toast.makeText(MainActivity.this, R.string.toastMessage, Toast.LENGTH_LONG);
toast.getView().setBackgroundColor(Color.parseColor("#F6AE2D"));
toast.show();
<?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>
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 toastView = inflater.inflate(R.layout.toast, null);
Toast toast = new Toast(context);
toastView.setView(toast);
// Set layout to toast
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
toast
? - shareefToast toast= Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want
toast.show();
val toast = Toast.makeText(context, "Text", Toast.LENGTH_SHORT)
toast.view.background.setTintList(ContextCompat.getColorStateList(context, android.R.color.darker_gray))
toast.show()
如何在JAVA中更改默认的烤面包消息颜色和背景颜色。您可以通过以下方式更改烤面包消息颜色和背景颜色。
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 = 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();
Kotlin 版本:
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()