问题
I have a custom toast notification which has an image and text. The custom toast works fine however I am wondering how do I make my custom toast inherit the default toasts look and feel? I want it to look like the default one with the nice rounded corners and borders.
This is what my custom toast looks like.
<?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>
回答1:
I use this in one of my apps. Change a few things around and it should work for you too.
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();
回答2:
To get the default background with the nice rounded corners (on lollipop) use:
android:background="@android:drawable/toast_frame"
or
android:background="?android:attr/toastFrameBackground"
It will give the Toast background depending on the version of Android, if you want the latest I suggest looking for the toast_frame.9.png file under ..sdk\platforms\android-[latest version]\data\res\drawable-[density]
Text Style:
android:textAppearance="@android:style/TextAppearance.Toast"
android:textColor="@android:color/bright_foreground_dark"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
Source: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/transient_notification.xml
回答3:
Try this
<style name="Themename" parent="android:Theme.dialog">
from developer.android.com/guide/topics/ui/themes.html
回答4:
Try below code.
Inflate the view from XML layout and name it as inflated_xml_view.
Toast toastView = new Toast(this);
toastView.setView(inflated_xml_view);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0,0);
toastView.show();
来源:https://stackoverflow.com/questions/8686398/android-custom-toast-notification-inherit-default-toast