【问题标题】:Toast background changing to match Activity's ThemeToast 背景更改以匹配 Activity 的主题
【发布时间】:2011-10-24 14:51:06
【问题描述】:

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

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

【问题讨论】:

    标签: android themes


    【解决方案1】:

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

    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();
    

    或者您可以实例化一个完全自定义的 toast:

    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();
    

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

    【讨论】:

    • 我认为 Javacadabra 的答案更好
    • 我读错问题了吗?但问题是如何防止它被定制,而您是在说明如何定制它?
    • @WORRMS,你是对的,但是...就主题更改而言,任何不应用此主题的 toast 都是自定义 toast(因为我们需要“重新设置样式”它回来了)
    【解决方案2】:

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

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

    Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
    

    与此相反(假设消息是从视图中调用的):

    Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
    

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

    Toast background color being changed

    【讨论】:

    • 很好的评论,谢谢!顺便说一句,第一个和接受的答案对我不起作用,但您的解决方案可以。
    • 谢谢,我和 OP 有同样的问题,这对我很有用!
    • 这是一个非常简单的解决方案,可以避免创建您自己的 toast 或乱用styles.xml。我喜欢!
    • 对我来说 Toast.makeText(getBaseContext(), "Text", Toast.LENGTH_SHORT).show(); 工作!
    【解决方案3】:

    这里有完整的示例,用于跨活动的自定义 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());
            }
        }
    

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      相关资源
      最近更新 更多