约束布局中的底部 Sheet 行为
Bottom Sheet Behaviour in Constraint Layout
我有一个简单的应用程序,我正在尝试添加底部 Sheet 行为。我想保留 Constraint Layout 但我也想获得 Bottom Sheet Behavior。我不想要协调器布局和底部 Sheet 对话框。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:enabled="false"
android:onClick="btn"
android:text="@string/btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="@layout/activity_shop"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_shop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/shop"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="false"
app:behavior_peekHeight="48dp">
<ImageView
android:id="@+id/arrow"
android:layout_width="match_parent"
android:padding="0dp"
android:layout_height="wrap_content"
android:contentDescription="@string/arrow"
app:srcCompat="@drawable/baseline_keyboard_arrow_up_white_48dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
我需要更改什么才能使它正常工作?
只需添加
app:layout_constraintBottom_toBottomOf="parent"
给你activity_shop.xml根
并将其高度更改为 wrap_content
要使用这样的层次结构来使用约束布局:
<CoordinatorLayout>
// *ViewGroup containing your fixed screen*
<Linear/Relative/Constraint Layout>
</Linear/Relative/Constraint Layout>
// *ViewGroup containing your bottom sheet*
<ConstraintLayout
app:behavior_hideable="false"
app:behavior_peekHeight="90dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" >
</Constraintlayout>
</CoordinatorLayout>
我有一个简单的应用程序,我正在尝试添加底部 Sheet 行为。我想保留 Constraint Layout 但我也想获得 Bottom Sheet Behavior。我不想要协调器布局和底部 Sheet 对话框。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:enabled="false"
android:onClick="btn"
android:text="@string/btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
layout="@layout/activity_shop"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_shop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/shop"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="false"
app:behavior_peekHeight="48dp">
<ImageView
android:id="@+id/arrow"
android:layout_width="match_parent"
android:padding="0dp"
android:layout_height="wrap_content"
android:contentDescription="@string/arrow"
app:srcCompat="@drawable/baseline_keyboard_arrow_up_white_48dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
我需要更改什么才能使它正常工作?
只需添加
app:layout_constraintBottom_toBottomOf="parent"
给你activity_shop.xml根
并将其高度更改为 wrap_content
要使用这样的层次结构来使用约束布局:
<CoordinatorLayout>
// *ViewGroup containing your fixed screen*
<Linear/Relative/Constraint Layout>
</Linear/Relative/Constraint Layout>
// *ViewGroup containing your bottom sheet*
<ConstraintLayout
app:behavior_hideable="false"
app:behavior_peekHeight="90dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" >
</Constraintlayout>
</CoordinatorLayout>