【问题标题】:Android KitKat: Snackbar is not in the bottom of the screenAndroid KitKat:Snackbar 不在屏幕底部
【发布时间】:2018-02-25 13:03:36
【问题描述】:

我在我的应用程序中添加了一个 Snackbar。问题是在 API 19 中它不在屏幕底部。

在 API 21 中没问题。这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

<data />
 <android.support.design.widget.CoordinatorLayout
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <EditText

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/home_search_input_hint"
            android:inputType="text"
            android:maxLength="30"
            android:maxLines="1"/>

    </android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>

还有我的OnCreate

 @Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_home);
    super.onCreate(savedInstanceState);
    // binding
    binding = DataBindingUtil.setContentView(this, R.layout.activity_home);


    // snackbar test
    Snackbar snackbar = Snackbar.make(binding.root, "Snackbar", Snackbar.LENGTH_INDEFINITE);

    snackbar.show();
}

你有什么解决办法吗?

更新:看起来底部的边距真的是随机的,我重新运行模拟器并看到这个。

还有这个

【问题讨论】:

    标签: android android-snackbar snackbar


    【解决方案1】:

    这是一个bug,仍未修复。

    解决方法 - 棒棒糖前设备上的小延迟。 UI 上的延迟不明显。

    将这些方法放在你的基础片段和基础活动中并在任何地方使用:

    protected ViewGroup getRootView() {
        return (ViewGroup) getView().getParent();
    }
    
    protected void showSnackbar(@StringRes int resId) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            new Handler().postDelayed(() -> Snackbar.make(getRootView(), resId, Snackbar.LENGTH_SHORT).show(), 200);
        } else {
            Snackbar.make(getRootView(), resId, Snackbar.LENGTH_SHORT).show();
        }
    }
    
    protected void showSnackbar(String message) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            new Handler().postDelayed(() -> Snackbar.make(getRootView(), message, Snackbar.LENGTH_SHORT).show(), 200);
        } else {
            Snackbar.make(getRootView(), message, Snackbar.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

      【解决方案2】:

      要解决此问题,请在显示 Snackbar 之前使用一小段时间延迟。如果您想在屏幕显示时立即显示 Snackbar,则需要使用 onCreateView()onCreate()

      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              // For com.android.support:design:v26 or v27, use a small
              // time delay, to prevent bottom gap bug on Android 4.4
              if (isAdded()) { snackbar.show(); }
          }
      }, 500);
      

      由于您有时间延迟,请记住通过检查 isAdded() 或检查 getActivity() != null 来防止 null Activity。

      【讨论】:

      • 对我来说 200 毫秒就足够了
      【解决方案3】:

      如果您使用26或27支持,可能是这种已知的错误:Google Issue Tracker: Snackbar wrong placement on the Android 4x versions

      【讨论】:

      • 它发生在Android 4.1和4.2上。 span>
      【解决方案4】:

      可以通过将显示小吃栏的代码移至 onGlobalLayout() 来避免此问题,如下所示。

      binding.root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
              {
                  @Override
                  public void onGlobalLayout()
                  {
                      // snackbar test
                      Snackbar snackbar = Snackbar.make(binding.root, "Snackbar", Snackbar.LENGTH_INDEFINITE);
                      snackbar.show();
      
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                          binding.root.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                      } else {
                          binding.root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                      }
                  }
              });
      

      【讨论】:

      • 当快餐栏被按钮点击等外部事件触发时,此解决方案如何工作?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2013-07-14
      • 2011-08-10
      • 2016-01-27
      • 2014-09-29
      • 2016-10-14
      相关资源
      最近更新 更多