【问题标题】:Android: how to make frame layout covering other UI components from the bottom?Android:如何使框架布局从底部覆盖其他 UI 组件?
【发布时间】:2025-11-26 11:35:01
【问题描述】:

我想显示在视图框架布局的底部,该布局在 FrameLayout 内部隐藏了一些布局,并在单击一些按钮后显示。我想将 FrameLayout 从屏幕底部开始显示在所有其他视图之上。

请问我怎样才能以正确的方式做到这一点?

非常感谢您的任何建议。

【问题讨论】:

  • 你的意思是其他 UI 组件
  • 能否请您发布到目前为止您尝试过的内容?xml 文件等。
  • 根容器可以是RelativeLayout,然后将android:layout_alignParentBottom="true" 添加到要对齐到屏幕底部的View

标签: android android-studio user-experience android-framelayout


【解决方案1】:

我已经实现了类似于您想要实现的功能...首先您应该将整个 Activity 的 Fragment 布局放在 CoordinatorLayout 中。然后,您应该将要显示在所有其他视图之上的 FrameLayout 作为最后一个视图放置在 CoordinatorLayout 中。您可以给 FrameLayout 一个固定的高度(200 像素)和与底部边距相同的负值(-200 像素),这样一开始就看不到它。单击某个按钮后,您可以为所需的视图(在本例中为 FrameLayout)设置动画,以覆盖从屏幕底部开始的所有其他视图。

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

//Your views here

<FrameLayout //Your FrameLayout
    android:layout_width="match_parent"
    android:layout_height="200px"        android:layout_marginBottom="-200px">

    //Place whatever views you prefer here 

</FrameLayout>

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

【讨论】:

    【解决方案2】:

    由于 Android Support Library 23.2 您可以使用材料 Bottom Sheets,例如 Google 地图:

    包括支持设计库(使用最新版本):

    compile 'com.android.support:design:24.1.1'
    

    您应该在CoordinatorLayout 中使用一个知道嵌套滚动的组件,例如NestedScrollView(顺便扩展了FrameLayout)和RecyclerView,并将此行为添加到其中:

        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    

    让它在底部稍微可见:

        app:behavior_peekHeight="64dp"
    

    【讨论】: