如何在Android中自定义Toast的背景,背景颜色和文本颜色

如何在Android中自定义Toast的背景,背景颜色和文本颜色,第1张

概述我想通过修改默认Toast来自定义我的吐司而不创建自定义布局.我希望红色为吐司的背景,白色为吐司的文字颜色,我想让我的吐司的背景更大,默认吐司.当我运行我的应用程序时,我的吐司没有任何变化,它仍然显示在默认的吐司中. 这就是我自定义吐司的方式: if (seriesSelection == null) { Toast toast = Toast.makeText(getApplicatio 我想通过修改默认Toast来自定义我的吐司而不创建自定义布局.我希望红色为吐司的背景,白色为吐司的文字颜色,我想让我的吐司的背景更大,默认吐司.当我运行我的应用程序时,我的吐司没有任何变化,它仍然显示在默认的吐司中.

这就是我自定义吐司的方式:

if (serIEsSelection == null) {    Toast toast = Toast.makeText(getApplicationContext(),"tIDak ada chart yang dipilih",Toast.LENGTH_SHORT);    toast.setGravity(Gravity.CENTER,50,50);    toast.getVIEw().setpadding(10,10,10);    toast.getVIEw().setBackgroundcolor(color.RED);    TextVIEw text = (TextVIEw) toast.getVIEw().findVIEwByID(androID.R.ID.message);    text.setTextcolor(color.WHITE);    text.setTextSize(14);} else {    Toast toast=  Toast.makeText(            getApplicationContext(),"Nilai " + ListData.get(serIEsSelection.getPointIndex()).getInuNilai()+            "  tanggal " + ListData.get(serIEsSelection.getPointIndex()).getTanggal(),Toast.LENGTH_SHORT);     toast.setGravity(Gravity.CENTER,10);    toast.getVIEw().setBackgroundcolor(color.RED);    text.setTextcolor(color.WHITE);    text.setTextSize(14);    toast.show();}
解决方法 您可以使用自定义视图为自定义视图充气并使用toast.setVIEw(布局).

例:

LayoutInflater inflater = getLayoutInflater();VIEw layout = inflater.inflate(R.layout.custom_toast,(VIEwGroup) findVIEwByID(R.ID.toast_layout_root));TextVIEw text = (TextVIEw) layout.findVIEwByID(R.ID.text);text.setText("This is a custom toast");Toast toast = new Toast(getApplicationContext());toast.setGravity(Gravity.CENTER_VERTICAL,0);toast.setDuration(Toast.LENGTH_LONG);toast.setVIEw(layout);toast.show();

还有你的xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"              androID:ID="@+ID/toast_layout_root"              androID:orIEntation="horizontal"              androID:layout_wIDth="fill_parent"              androID:layout_height="fill_parent"              androID:padding="8dp"              androID:background="#DAAA"              >    <ImageVIEw androID:src="@drawable/droID"               androID:layout_wIDth="wrap_content"               androID:layout_height="wrap_content"               androID:layout_marginRight="8dp"               />    <TextVIEw androID:ID="@+ID/text"              androID:layout_wIDth="wrap_content"              androID:layout_height="wrap_content"              androID:textcolor="#FFF"              /></linearLayout>

更多信息 @

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

在你的if和else部分代码(单独)中显示它以红色背景和白色文本颜色显示吐司.我没有看到任何问题.但是,如果您需要自定义,可以使用自定义布局并使布局膨胀并将视图设置为toast.

编辑:

你的textvIEw

TextVIEw text = (TextVIEw) toast.getVIEw().findVIEwByID(androID.R.ID.message);

在if部分初始化,而在其他部分textvIEw未初始化.

在if和else代码之外初始化textvIEw.

检查这个名为crouton的库,您可能会发现它很有用

https://github.com/keyboardsurfer/Crouton

总结

以上是内存溢出为你收集整理的如何在Android中自定义Toast的背景,背景颜色和文本颜色全部内容,希望文章能够帮你解决如何在Android中自定义Toast的背景,背景颜色和文本颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存