Android 安卓:有没有一个好方法可以创建一个类似于吐司的对话框?
我想向用户显示一些信息,但我不想在用户点击其他位置或按下后退按钮之前忽略这些信息。我意识到一个明显的选择是在对话框中显示文本(与祝酒词相反)。我希望我的对话框类似于系统吐司。我知道我可以只复制Android 安卓:有没有一个好方法可以创建一个类似于吐司的对话框?,android,Android,我想向用户显示一些信息,但我不想在用户点击其他位置或按下后退按钮之前忽略这些信息。我意识到一个明显的选择是在对话框中显示文本(与祝酒词相反)。我希望我的对话框类似于系统吐司。我知道我可以只复制transient\u notification.xml布局(及其相关资源),但由于Toast样式因设备和操作系统而异,因此不可能产生很好的匹配 那么,有没有一种很好的方法来创建一个继承系统Toast样式的对话框呢?您可能想试试,它是可定制的,可以在触摸时取消。两个可能的建议。一种可能是使用PopupWin
transient\u notification.xml
布局(及其相关资源),但由于Toast样式因设备和操作系统而异,因此不可能产生很好的匹配
那么,有没有一种很好的方法来创建一个继承系统Toast样式的对话框呢?您可能想试试,它是可定制的,可以在触摸时取消。两个可能的建议。一种可能是使用PopupWindow和自定义XML。实施起来应该不会太难。另一种选择是使用开源项目cruton()中的Toast替换系统。基本上,它提供了一个更美观的用户界面,而且我知道有一些选项等待用户单击后才放弃。您可以在Play store中下载他们的演示。您可以使用此小部件(不带撤消按钮),如图所示 或者您可以使用自定义布局 显示祝酒词的自定义方法
public static Toast currentToast;
/**
* Use a custom display for Toasts.
*
* @param message
*/
public static void customToast(String message) {
// Avoid creating a queue of toasts
if (currentToast != null) {
// Dismiss the current showing Toast
currentToast.cancel();
}
//Retrieve the layout Inflater
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Assign the custom layout to view
View layout = inflater.inflate(R.layout.custom_toast, null);
//Return the application context
currentToast = new Toast(context.getApplicationContext());
//Set toast gravity to center
currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
//Set toast duration
currentToast.setDuration(Toast.LENGTH_LONG);
//Set the custom layout to Toast
currentToast.setView(layout);
//Get the TextView for the message of the Toast
TextView text = (TextView) layout.findViewById(R.id.text);
//Set the custom text for the message of the Toast
text.setText(message);
//Display toast
currentToast.show();
// Check if the layout is visible - just to be sure
if (layout != null) {
// Touch listener for the layout
// This will listen for any touch event on the screen
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// No need to check the event, just dismiss the toast if it is showing
if (currentToast != null) {
currentToast.cancel();
// we return True if the listener has consumed the event
return true;
}
return false;
}
});
}
}
和自定义的_toast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="@drawable/custom_toast_shape">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="16sp"
android:text="@string/app_name"
/>
<View
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/blue"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:maxEms="15"
android:gravity="center_horizontal"
android:id="@+id/text"
/>
</LinearLayout>
编辑
我对这个方法做了一点修改。现在,它不会创建一个祝酒队列,并且会像正常的祝酒一样在触摸时被解除
如果其他人可以改进它,请随意操作:)toast就是这样工作的。您可以创建自定义对话框,并根据需要设置其样式。具有自定义主题。应该不是问题
Utils.customToast("Just a test to see if toast is working!\nPerfect");