【问题标题】:Android Persist A SnackBar Across ActivitiesAndroid 跨活动持久化 SnackBar
【发布时间】:2020-02-02 16:04:27
【问题描述】:

情况:用户已登录,您显示一个显示已成功登录的小吃店,然后导航到另一个意图,但问题是当您导航时小吃店被取消/销毁。无论您导航到什么活动,我们如何像 Toast 那样在活动中持久保存它......它保持活力和健康并遵循它的超时。

【问题讨论】:

  • 使用 Fragments 会解决这个问题吗?只需在Activity中切换出一个Fragment,Snackbar自然会持续存在。我试图弄清楚如何在旋转中保持 Snackbar ! :(
  • 是的,这已经击中了我,但它不是我的选择,因为我的大部分东西都针对活动进行了优化,无论如何你可以尝试使用 onpause
  • 你也必须在里面放很多逻辑
  • 是的,我发现只需在 onResume 中从头开始重建 SnackBar 就可以了。我总是将我的应用程序状态保留在我的活动之外,因此小吃栏只会在 onresume 中填充自己,这与它在自发显示时填充自己的方式相同。
  • 不要在你的情况下使用 SnackBar。

标签: android user-interface


【解决方案1】:

您可以制作类似于小吃店的自定义吐司:

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): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

在这个 run 函数中环绕 showCustomToast 函数中的代码,

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {

    }

});

【讨论】:

  • 谢谢,如果您不需要点击监听器而只需要显示一条消息,则可以更好地回答。
【解决方案2】:

围绕您的视图组制作一个包装器,并将其添加到您的每个布局中。您还可以使用窗口覆盖。见here

【讨论】:

    【解决方案3】:

    事实上,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(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多