我在 BottomSheetDialogFragment 上放置了一个布局。在布局中,3 个可见的小部件交替出现。所以一个小部件 setVisibilty(View.VISIBLE),其他小部件 setVisibility(View.GONE)。但是当widgets setVisibility(View.GONE),BottomSheetDialogFragment会弹出到TOP。滑动BottomSheetDialogFragment后,它会立即移动到底部。我不知道这种情况的原因。有人告诉我 setVisibility(View.GONE) 不会影响 BottomSheetDialogFragment 的生命周期。所以问题可能是 layoutPrarms 或行为的高度。
这是 XML 文件。有问题吗?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@drawable/bottom_sheet_background">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/transition_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="20dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayoutAll"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:srcCompat="@drawable/ic_dashboard_black_24dp" />
<EditText
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英文"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView1"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<EditText
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="36dp"
android:text="念書"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView1"
app:layout_constraintEnd_toStartOf="@+id/guideline4" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="16dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/primaryColor"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline2" />
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/button2" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayoutAll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:animateLayoutChanges="true"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayoutText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout03"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:paddingEnd="8dp"
app:boxStrokeColor="@color/primaryDarkColor"
app:hintTextColor="@color/colorAccent"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/starting_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:drawableRight="@drawable/schedule_24px"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Starts"
android:imeOptions="actionNext"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="12dp"
android:paddingBottom="16dp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout04"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingStart="8dp"
app:boxStrokeColor="@color/primaryDarkColor"
app:hintTextColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/starting_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:drawableRight="@drawable/today_24px"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Date"
android:imeOptions="actionNext"
android:longClickable="false"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="12dp"
android:paddingBottom="16dp"
android:text="2021/1/7" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout05"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:paddingEnd="8dp"
android:paddingBottom="24dp"
app:boxStrokeColor="@color/primaryDarkColor"
app:hintTextColor="@color/colorAccent"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/TextInputLayout03">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ending_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:drawableRight="@drawable/schedule_24px"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Ends"
android:imeOptions="actionNext"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="12dp"
android:paddingBottom="16dp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout06"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:paddingStart="8dp"
android:paddingBottom="24dp"
app:boxStrokeColor="@color/primaryDarkColor"
app:hintTextColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline9"
app:layout_constraintTop_toBottomOf="@+id/TextInputLayout04">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ending_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:drawableRight="@drawable/event_24px"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Date"
android:imeOptions="actionNext"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="12dp"
android:paddingBottom="16dp"
android:text="2021/1/8" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<TimePicker
android:id="@+id/TimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<DatePicker
android:id="@+id/DatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是Java代码。
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = this.getDialog().getWindow();
assert window != null;
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.windowAnimations = R.style.BottomDialogAnimation;
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
final View view = inflater.inflate(R.layout.new_event, null);
uiInit(view);
setOnClickListener();
varInit();
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.BottomSheetDialogTheme);
}
}
PS。对不起,我的英语不好。如果有任何问题,请告诉我。我会改进的。感谢大家