2017-09-16 21 views
3

私のアプリにSnackbarを追加します。問題は、API 19では画面の最下部にないことです。 API 21でAndroid KitKat:Snackbarが画面の一番下にありません

enter image description here

それは大丈夫です。ここに私のレイアウトは

<?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(); 
} 

であるあなたがそれを修正する方法任意のアイデアを持っていますか?

更新:下からのマージンは本当にランダムなようですが、私はエミュレータを再実行し、これを見ます。

enter image description here

この

enter image description here

答えて

1

この問題は、次のように)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); 
       } 
      } 
     }); 
関連する問題