android - 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>
解决方案
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>
推荐阅读
- java - 我可以在 Android Sdk<=26 中使用 java 时间库的所有功能吗
- laravel - bootstrap和laravel的图片轮播中只显示一张图片
- flutter - Flutter 理解模型
- ios - CocoaPods 找不到 pod“UMCore”的兼容版本
- asp.net-mvc - 如何为导航属性创建 MVC 视图(一对多、多对多)
- html - 行间距与表格中的移动大小和文本有关。我需要允许单词中断和字距调整以防止单词被砸在彼此之上
- vue.js - this.$getLocation 不是一个函数,这是 nuxt 告诉我的。我如何让它识别它为一个?
- javascript - NodeJS如何调用从另一个文件夹返回字符串的函数
- javascript - 如何根据某些条件删除结帐页面上付款方式步骤上的“下订单”按钮 - BigCommerce?
- python - Cloud Build App Engine 失败 INVALID_ARGUMENT