改变吐司背景以匹配活动的主题

Mal*_*ist 21 android themes

我为他们所有人都使用的活动创建了一个自定义主题.在主题中,我设置了android:background,这恰好会导致任何对话或Toast消息看起来很奇怪.

如何防止吐司和其他对话框吸收主题的属性?

Dmi*_*try 51

您可以通过以下代码轻松创建自定义吐司:

Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_bkg);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*here you can do anything with text*/
toast.show();
Run Code Online (Sandbox Code Playgroud)

或者您可以实例化完全自定义的吐司:

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.custom_layout, null);
toast.setView(view);
toast.show();
Run Code Online (Sandbox Code Playgroud)

对话定制是一个更复杂的例程.但也有类似的解决方法.

  • 在我看来,Javacadabra的答案更好 (3认同)

Jav*_*bra 28

我意识到这个问题已得到解答,而且这个帖子在这个阶段已经很老了.但是我想我会为遇到这个问题的人留下答案.

我今天遇到了这个问题的麻烦,我解决它的方式是显示我的Toast消息,如下所示:

Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

与此相反(假设从View中调用消息):

Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

它解决了我遇到的问题.无论如何希望它有所帮助.这是关于类似主题的问题的链接.

吐司背景颜色被改变

  • 对我来说`Toast.makeText(getBaseContext(),"Text",Toast.LENGTH_SHORT).show();`工作! (4认同)
  • 好评,谢谢!顺便说一句,第一个和接受的答案对我不起作用,但你的解决方案确实如此. (2认同)
  • 谢谢,我和OP有同样的问题,这对我很有帮助! (2认同)
  • 这是一个非常简单的解决方案,可以避免创建自己的toast或者不得不使用styles.xml.我喜欢! (2认同)

gul*_*yuz 5

这里有完整的示例,可用于跨活动的自定义Toast。

displayToast

// display customized Toast message
    public static int SHORT_TOAST = 0;
    public static int LONG_TOAST = 1;
    public static void displayToast(Context caller, String toastMsg, int toastType){

        try {// try-catch to avoid stupid app crashes
            LayoutInflater inflater = LayoutInflater.from(caller);

            View mainLayout = inflater.inflate(R.layout.toast_layout, null);
            View rootLayout = mainLayout.findViewById(R.id.toast_layout_root);

            ImageView image = (ImageView) mainLayout.findViewById(R.id.image);
            image.setImageResource(R.drawable.img_icon_notification);
            TextView text = (TextView) mainLayout.findViewById(R.id.text);
            text.setText(toastMsg);

            Toast toast = new Toast(caller);
            //toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            if (toastType==SHORT_TOAST)//(isShort)
                toast.setDuration(Toast.LENGTH_SHORT);
            else
                toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(rootLayout);
            toast.show();
        }
        catch(Exception ex) {// to avoid stupid app crashes
            Log.w(TAG, ex.toString());
        }
    }
Run Code Online (Sandbox Code Playgroud)

toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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>
Run Code Online (Sandbox Code Playgroud)