首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:如何设置Toast文本的颜色

Android:如何设置Toast文本的颜色
EN

Stack Overflow用户
提问于 2011-07-14 09:57:59
回答 8查看 70.3K关注 0票数 53

我使用以下代码显示一条toast消息作为if语句的结果:

代码语言:javascript
复制
Toast.makeText(getBaseContext(), "Please Enter Price", Toast.LENGTH_SHORT).show();

它在白色背景上显示为白色文本,因此无法阅读!我的问题是,如何更改吐司文本的颜色?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-07-14 10:01:51

您可以创建自定义Toast view以满足您的要求。请参阅http://developer.android.com/guide/topics/ui/notifiers/toasts.html上标题为“创建自定义Toast视图”的部分

票数 22
EN

Stack Overflow用户

发布于 2011-07-14 10:03:54

您可能希望创建自定义Toast

代码语言:javascript
复制
<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="10dp"
          android:background="#DAAA"
          >
<ImageView android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:layout_marginRight="10dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />
</LinearLayout>

-

代码语言:javascript
复制
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Source

票数 16
EN

Stack Overflow用户

发布于 2015-03-11 15:51:06

更改吐司的背景色和吐司文本的背景色的最简单方法是:

代码语言:javascript
复制
View view;
TextView text;
Toast toast;
toast.makeText(this, resId, Toast.LENGTH_SHORT);
view = toast.getView();
text = (TextView) view.findViewById(android.R.id.message);
text.setTextColor(getResources().getColor(R.color.black));
text.setShadowLayer(0,0,0,0);
view.setBackgroundResource(R.color.white);
toast.show();
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6687666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档