如何在Android中自定义Toast?

问题描述 投票:8回答:6

是否可以在Android中制作自定义Toast。就像我们可以放置它图像图标和放置按钮。

android toast
6个回答
2
投票

您还可以使用常规的makeText()并处理getView()以设置旁边的图像以查看下一个。

Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));

1
投票

您可以使用setView将任何视图放入Toast中。但是,我不太清楚你为什么要在其中放置一个按钮,因为Toast会迅速消失。摘自官方开发者网站:

向用户显示视图时,将显示为应用程序上的浮动视图。它永远不会得到关注。用户可能正在键入其他内容。这个想法尽可能不引人注目,同时仍向用户显示您希望他们看到的信息。

所以吐司应该只用于显示信息。对于更复杂的交互,您可以使用对话框。


1
投票

Toast是非焦点能力。添加按钮没有意义。但是,您可以显示信息。您还可以控制其可见性,这意味着您可以通过在Toast类中进行少量更改来隐藏和显示。


1
投票

显然可以在android中创建自定义吐司。请查看我的博客.http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/我已经详细解释过了。


0
投票

XML文件

enter code here`<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="8dp"
          android:background="#DAAA"
          >
<ImageView android:src="@drawable/droid"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="8dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />

'

JAVA CODE

 LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("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();

0
投票

烤面包可以定制,以显示在顶部,底部,中间,左,右等不同的地方

 toast.setGravity(Gravity.TOP , 0, 0); // for example setting gravity to top

更多详情[这里](http://androidcoding.in/2016/05/31/android-tutorial-custom-toast/“android custom toast”)

© www.soinside.com 2019 - 2024. All rights reserved.