我是Android编程的新手。在Android上显示自定义吐司通知的简单示例是什么?
发布于 2012-07-02 14:10:33
使用自定义Toast的以下代码。这可能会对你有帮助。
toast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#DAAA" >
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#FFF" />
</LinearLayout>
MainActivity.java
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! 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();
查看下面的链接,也可以获得定制的Toast。
Custom Toast with Analog Clock
YouTube: Creating Custom Toast With Button in Android Studio
发布于 2012-07-02 14:11:28
请参见链接here。你会找到你的解决方案。并尝试:
创建自定义Toast视图
如果一条简单的文本消息还不够,您可以为您的吐司通知创建自定义布局。若要创建自定义布局,请在可扩展标记语言或应用程序代码中定义视图布局,并将根视图对象传递给setView (视图)方法。
例如,您可以使用以下XML (另存为toast_layout.xml)为右侧屏幕截图中可见的toast创建布局:
<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>
注意,LinearLayout元素的ID是"toast_layout“。您必须使用此ID从XML扩展布局,如下所示:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! 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();
首先,使用getLayoutInflater() (或getSystemService())检索LayoutInflater,然后使用inflate(int,ViewGroup)从XML扩展布局。第一个参数是布局资源ID,第二个参数是根视图。您可以使用这个放大的布局来查找布局中的更多视图对象,因此现在可以捕获并定义ImageView和TextView元素的内容。最后,使用Toast(Context)创建一个新toast,并设置Toast的一些属性,如重力和持续时间。然后调用setView(视图)并将膨胀的布局传递给它。现在,您可以通过调用show()来显示具有自定义布局的吐司。
注意:不要使用Toast的公共构造函数,除非你打算用setView(视图)来定义布局。如果没有要使用的自定义布局,则必须使用makeText(Context,int,int)来创建Toast。
发布于 2017-09-12 12:59:59
toast、custom_toast.xml
的自定义布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Custom Toast"
android:gravity="center"
android:id="@+id/custom_toast_text"
android:typeface="serif"
android:textStyle="bold"
/>
</LinearLayout>
和Java方法(只需将toast消息传递给此方法):
public void toast(String message)
{
Toast toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(R.layout.image_custom, null);
TextView textView = (TextView) view.findViewById(R.id.custom_toast_text);
textView.setText(message);
toast.setView(view);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
https://stackoverflow.com/questions/11288475
复制相似问题