首页 > 解决方案 > Android 底页总是贴在左上角

问题描述

我正在尝试创建一个可滑动的底部工作表,并试图将底部工作表(线性布局)限制在主视图的底部,但无法这样做。它只是粘在左上角,同时保持其尺寸。难道我做错了什么?

webview.xml(主视图)

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        layout="@layout/bottom_sheet"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

bottom_sheet.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:layout_height="wrap_content"
    android:id="@+id/bottom_sheet"
    android:background="@android:color/transparent"
    app:layout_behavior="@string/bottom_sheet_behavior"
    app:behavior_hideable="true"
    app:behavior_peekHeight="25dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="?android:attr/colorBackground"
        app:cardCornerRadius="1dp"
        app:cardElevation="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="#808080"/>

            <WebView
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="100dp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

标签: androidxml

解决方案


BottomSheetBehavior 只能在CoordinatorLayout这样的内部使用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include layout="@layout/bottom_sheet" />

</android.support.design.widget.CoordinatorLayout>

在您的 bottom_sheet 中,您必须正确设置布局行为,如下所示:

<?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:layout_height="wrap_content"
    android:id="@+id/bottom_sheet"
    android:background="@android:color/transparent"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    app:behavior_hideable="true"
    app:behavior_peekHeight="25dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="?android:attr/colorBackground"
        app:cardCornerRadius="1dp"
        app:cardElevation="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_gravity="center"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="#808080"/>

            <WebView
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="100dp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

推荐阅读