2017-08-14 57 views
13

我試圖用TextInputLayout創建一個BottomSheetDialogFragment。我將此BottomSheet設置爲adjustResize以防止鍵盤覆蓋TextInputLayout。事情是,我得到不同的Android版本不同的行爲。BottomSheetDialogFragment中的ADJUST_RESIZE的不同行爲

這是佈局:

<android.support.constraint.ConstraintLayout 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" 
android:id="@+id/linearLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.TextInputLayout 
    android:id="@+id/textInputLayout" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="16dp" 
    android:layout_marginEnd="16dp" 
    android:layout_marginStart="16dp" 
    android:layout_marginTop="16dp" 
    android:background="@android:color/darker_gray" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.0"> 

    <android.support.design.widget.TextInputEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="hint" /> 
</android.support.design.widget.TextInputLayout> 

此所述BottomSheetDialogFragment:

public class TestFragment extends BottomSheetDialogFragment { 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

    return inflater.inflate(R.layout.fragment_test, container, true); 
} 

}

這是所期望的結果:

enter image description here

這是我得到的一些版本的結果:

enter image description here

我得到了想要的結果有:

  • 銀河S6採用Android 7.0
  • 仿真器< = Android 5.1

而且沒有理想的結果有:

  • 的Nexus 5採用Android 7.1.2(LineageOS 14.1)
  • 仿真器=>在Android 6.0

有誰知道爲什麼發生這種情況或如何解決它?

在此先感謝!

乾杯。

回答

2

這個問題浪費了我1小時。不要使用ADJUST_RESIZE

只需將您的佈局包裹在NestedScrollView之內。

沒有什麼更少。問題解決了。

這裏是XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.constraint.ConstraintLayout 
     android:id="@+id/linearLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/textInputLayout" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="16dp" 
      android:layout_marginEnd="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginTop="16dp" 
      android:background="@android:color/darker_gray" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintEnd_toEndOf="parent" 
      app:layout_constraintStart_toStartOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintVertical_bias="0.0"> 

      <android.support.design.widget.TextInputEditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="hint" /> 
     </android.support.design.widget.TextInputLayout> 

    </android.support.constraint.ConstraintLayout> 

</android.support.v4.widget.NestedScrollView> 

這裏是源代碼

public class DemoFragment extends BottomSheetDialogFragment { 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 

     @Override 
     public void onStateChanged(@NonNull View bottomSheet, int newState) { 
      if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
       dismiss(); 
      } 
     } 

     @Override 
     public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
     } 
    }; 

    @Override 
    public void setupDialog(Dialog dialog, int style) { 
     super.setupDialog(dialog, style); 
     View contentView = View.inflate(getContext(), R.layout.fragment_layout, null); 
     dialog.setContentView(contentView); 

     CoordinatorLayout.LayoutParams layoutParams = 
       (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams(); 
     CoordinatorLayout.Behavior behavior = layoutParams.getBehavior(); 
     if (behavior != null && behavior instanceof BottomSheetBehavior) { 
      ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     } 
    } 

} 
+0

我試過,但我得到了相同的結果。你能粘貼你使用的xml嗎?謝謝 – Buntupana

+0

@Buntupana更新了答案。請嘗試解決方案並讓我知道結果。:) –

+0

對於延誤感到抱歉,我在國外。我複製並粘貼了你的代碼,但仍然無法使用。我試過用Android O – Buntupana