我刚刚在我的模拟器中安装了Android (API 30)图像来尝试我的应用程序,当我试图设置一个Toast的背景色时,它就崩溃了。
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
这真的很奇怪,因为在Android (API 29)中工作非常完美。
我的build.gradle更新了安卓R (API 30)
compileSdkVersion 30
buildToolsVersion "30.0.1"
有什么新方法吗??
发布于 2020-07-13 22:22:19
谷歌表示,自从Android 11之后,定制祝酒词/吐司修改就不再受欢迎了,以“保护用户”。这就是为什么你的Android 30应用程序不能显示自定义祝酒的原因。
来自Android开发人员的文档:
不建议使用
自定义吐司视图。应用程序可以用makeText(android.content.Context,java.lang.CharSequence,int)
创建标准文本吐司。
发布于 2020-10-12 17:19:06
我发现从API 30开始显示自定义祝酒词的唯一方法是临时创建它们。
XML布局
根据需要自定义
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_activity">
<!--Ad hoc toast Textview-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_margin="18dp"
android:background="@drawable/ad_hoc_toast_background"
android:textColor="#1e1e1e"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:id="@+id/ad_hoc_toast_textview"
tools:text="Temporary message bla bla bla ..."/>
</RelativeLayout>
吐司背景 (ad_hoc_toast_background.xml)
根据需要自定义
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="220dp"
android:height="100dp"/>
<corners
android:radius="25dp"
/>
<solid
android:color="#e6ffffff"
/>
</shape>
</item>
</selector>
show_ad_hoc_toast() 定义方法
private void show_ad_hoc_toast(final TextView ad_hoc_toast_textview, String text){
//Set the text
ad_hoc_toast_textview.setText(text);
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(0f, 1f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation){}
@Override public void onAnimationRepeat(Animation animation){}
@Override public void onAnimationEnd(Animation animation){
//After 2250 millis -> hide the toast
new CountDownTimer(2250, 1) {
public void onTick(long millisUntilFinished){}
public void onFinish() {hide_ad_hoc_toast(ad_hoc_toast_textview);}
}.start();
}
});
//Make the view visible
ad_hoc_toast_textview.setVisibility(View.VISIBLE);
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
hide_ad_hoc_toast() 定义方法
private void hide_ad_hoc_toast(final TextView ad_hoc_toast_textview){
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(1f, 0f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) { }
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) {
//Make the view gone
ad_hoc_toast_textview.setVisibility(View.GONE);
}
});
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
在需要时从代码中调用方法
//Find ad_hoc_toast textview
TextView ad_hoc_toast_textview = findViewById(R.id.ad_hoc_toast_textview);
//Define the text to be shown
String text = "This is the custom toast message"
//Show the ad_hoc toast
show_ad_hoc_toast(ad_hoc_toast_textview, text);
结果
发布于 2021-02-02 06:51:14
你可以在吃烤面包之前检查一下
Toast toast = Toast.makeText(ctxt, msg, duration);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
}
toast.show();
https://stackoverflow.com/questions/62884286
复制相似问题