【问题标题】:How to display bottomSheet xml file in main activity?如何在主要活动中显示bottomSheet xml文件?
【发布时间】:2021-07-11 22:42:56
【问题描述】:

我正在尝试将我的bottom_sheet.xml 文件包含到我的活动activity_main.xml 文件中。我正在尝试通过使用<include layout="@layout/bottom_sheet"/>

来实现这一点

但是,我的activity_main.xml 文件中没有显示底部工作表。

activity_main.xml 文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.FaceDetector.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.FaceDetector.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

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


</androidx.coordinatorlayout.widget.CoordinatorLayout>

bottom_sheet.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"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:behavior_hideable="false"
    app:behavior_peekHeight="74dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/bottom_sheet_button"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:layout_margin="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view">

        <ImageView
            android:id="@+id/bottom_sheet_button_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@android:drawable/ic_menu_camera" />

        <ProgressBar
            android:id="@+id/bottom_sheet_butotn_progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone" />


    </FrameLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/bottom_sheet_recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view" />
</androidx.constraintlayout.widget.ConstraintLayout>

结果图片:

谢谢!

【问题讨论】:

  • 你放错地方了,把 放在 content_main 中。它会显示

标签: android xml android-studio bottom-sheet


【解决方案1】:

将您的 include 标签更改为bottom_sheet,如下面的activity_main.xml

通过将属性app:layout_behaviorbottom_sheet 的其他属性添加到include 标记中,并为您的include 标记指定宽度和高度

 <include
        layout="@layout/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_hideable="false"
        app:behavior_peekHeight="74dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />

并从bottom_sheet.xml 中删除不必要的属性:

app:behavior_hideable=""
app:behavior_peekHeight=""
app:layout_behavior="" 

所以你需要在include标签内添加所有bottom_sheet属性而不是布局

【讨论】:

  • 您好,谢谢您的回答。但是,移动属性对我不起作用。在研究了更多之后,我发现这个回答了我的问题:stackoverflow.com/questions/51617186/…
  • 是的,我没有注意到 app:layoout_behavior 对不起兄弟,祝我好运,我正在使用材料库
  • @CodingChap 当您设置app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" 而不是"android.support.design.widget.BottomSheetBehavior" 时,我的回答可能会有所帮助
  • 嗨,谢谢。我之前也更改了它,但我认为如果我将属性移动到包含标签而不是布局,这不会有什么不同。两种方式都适合我。
  • 好吧,如果其他人和你有同样的问题,可能会有用,因为其他帖子没有讨论这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多