答案 0 :(得分:2)
您可以制作类似于小吃店的自定义吐司:
custom_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toast_layout"
android:layout_gravity="bottom|center_horizontal"
android:background="#545454"
android:gravity="left|fill_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/toast_text"
android:layout_gravity="bottom"
android:textColor="#ffffff"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="8dp" />
</LinearLayout>
并以这种方式显示:
public void showCustomToast(String msg)
{
//inflate the custom toast
LayoutInflater inflater = getLayoutInflater();
// Inflate the Layout
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.toast_text);
// Set the Text to show in TextView
text.setText(msg);
Toast toast = new Toast(getApplicationContext());
//Setting up toast position, similar to Snackbar
toast.setGravity(Gravity.BOTTOM | Gravity.LEFT | Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
如果收到ERROR / AndroidRuntime(5176):引起:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
在此运行函数中包含showCustomToast函数内的代码,
this.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
答案 1 :(得分:0)
在视图组周围创建一个包装器,并将其添加到每个布局中。您还可以使用窗口覆盖。见here
答案 2 :(得分:0)
事实上,SnackBar不应该是持久的或堆叠的,因为它们高于屏幕上的其他元素。您可以在Google Design
中查看此规则但您可以在此处使用第三方Material Design库:rey5137/material
您可以通过调用以下功能之一来显示它:
public void show(Activity activity);
//parent should be FrameLayout or RelativeLayout so SnackBar can algin bottom.
public void show(ViewGroup parent);
// It only work if SnackBar is already attached to a parent view.
public void show();