改变Toast的颜色

时间:2016-04-26 20:24:19

标签: android

如何更改邮件Toast的颜色?

这是我的代码:

select * from(
select substring(Name,1,1), substring(Surname,1,1),
row_number() over (partition by Name,Surname order by Team)) from t1;

3 个答案:

答案 0 :(得分:8)

Toast toast = Toast.makeText(getApplicationContext(), "Correto!",
                Toast.LENGTH_SHORT);

TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
toastMessage.setTextColor(Color.RED);
toast.show();

答案 1 :(得分:3)

改变吐司的颜色,位置和背景颜色的方法是:

    Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
    View view=toast.getView();
    TextView  view1=(TextView)view.findViewById(android.R.id.message);
    view1.setTextColor(Color.YELLOW);
    view.setBackgroundResource(R.color.colorPrimary);
    toast.show();

对于逐行说明:https://www.youtube.com/watch?v=5bzhGd1HZOc

答案 2 :(得分:2)

创建自定义Toast布局,例如correct_toast.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">
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

然后在java代码中,用这个视图构造toast:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.correct_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, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

这样,您就可以更改背景颜色和/或更改文本颜色。