Android:自定义Toast通知继承默认Toast

Android:自定义Toast通知继承默认Toast,android,android-layout,toast,Android,Android Layout,Toast,我有一个自定义的toast通知,其中包含图像和文本。自定义吐司工作正常,但我想知道如何使自定义吐司继承默认吐司的外观和感觉?我希望它看起来像默认的一个漂亮的圆角和边框 这就是我定制的烤面包片的样子 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_lay

我有一个自定义的toast通知,其中包含图像和文本。自定义吐司工作正常,但我想知道如何使自定义吐司继承默认吐司的外观和感觉?我希望它看起来像默认的一个漂亮的圆角和边框

这就是我定制的烤面包片的样子

<?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/chatIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_chat"/>
    <TextView android:id="@+id/text"
        android:text="@string/unread_message_toast"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

试试这个

<style name="Themename" parent="android:Theme.dialog">


从developer.android.com/guide/topics/ui/themes.html

我在我的一个应用程序中使用它。改变一些事情,它也应该适合你

Toast ImageToast = new Toast(getBaseContext());
                LinearLayout toastLayout = new LinearLayout(
                        getBaseContext());
                toastLayout.setOrientation(LinearLayout.HORIZONTAL);
                ImageView image = new ImageView(getBaseContext());
                image.setImageResource(R.drawable.easter_egg);
                toastLayout.addView(image);
                ImageToast.setView(toastLayout);
                ImageToast.setDuration(Toast.LENGTH_SHORT);
                ImageToast.show();
试试下面的代码

从XML布局展开视图,并将其命名为展开的XML视图

    Toast toastView = new Toast(this);
    toastView.setView(inflated_xml_view);
    toastView.setDuration(Toast.LENGTH_LONG);
    toastView.setGravity(Gravity.CENTER, 0,0);
    toastView.show();

要获得具有漂亮圆角的默认背景(在棒棒糖上),请使用:

它将根据Android版本提供Toast背景,如果您想要最新版本,我建议您在..sdk\platforms\Android-[最新版本]\data\res\drawable-[density]下查找Toast\u frame.9.png文件

文本样式:

    android:textAppearance="@android:style/TextAppearance.Toast"
    android:textColor="@android:color/bright_foreground_dark"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"

来源:

谢谢您的回复,很抱歉我没有回复您。今晚我将尝试这个,并奖励一位获奖者。太棒了!非常灵活。这可以重用,而无需添加那些布局XML和管理元素ID。以前我用过充气机方法-不太容易重复使用+1这似乎不再有效。引发异常:java.lang.RuntimeException:此Toast不是使用Toast创建的。makeText()听起来是个不错的主意,但它不起作用。当所有标准的Android Toast消息都是四舍五入时,我仍然会得到四舍五入。我将Toast布局设置为layout.setBackground(getDrawable(android.R.drawable.Toast_frame));
android:background="?android:attr/toastFrameBackground"
    android:textAppearance="@android:style/TextAppearance.Toast"
    android:textColor="@android:color/bright_foreground_dark"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"