【发布时间】:2019-05-17 07:41:24
【问题描述】:
我正在尝试使用协调器布局创建一个屏幕,其中包括:
- 工具栏
- 片段,将使用导航 API 为每个页面替换
- 底部应用栏
但是,我正在努力以某种方式定位视图,因此应用程序的底栏不会与片段中的内容重叠。
请你帮我解决这个问题,好吗?据我所知,唯一的方法是在底部添加一个边距,但这可能在设备之间并不一致。
为了说明,此列表共有 25 项,但最后两项与应用的底栏重叠。
活动主要
<?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:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay"
android:animateLayoutChanges="true"
android:layout_above="@id/include">
<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/AppTheme.PopupOverlay"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:text="Test 1"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<com.google.android.material.tabs.TabItem
android:text="Test 2"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<include
layout="@layout/content_main"
android:id="@+id/include"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchorGravity="right|top"
app:layout_anchor="@+id/bar"
android:src="@drawable/ic_add_black_24dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
内容主xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<fragment
android:layout_width="411dp"
android:layout_height="627dp"
android:name="com.example.fitnessfatality.startScreen.StartFragment"
android:id="@+id/fragment"
/>
</FrameLayout>
【问题讨论】:
-
你为什么不使用 ConstraintLayout 呢?
-
我想过使用约束布局,但带有锚定 FAB 的底部应用栏仅适用于约束布局。我还想了解如何在找到解决此问题的方法后实现滚动行为。
-
找到什么了吗?我有同样的问题,只是单帧布局。
-
使用android:layout_marginBottom="?attr/actionBarSize" 或者bottomPadding 和cliptoadding="false" 可以从导航栏顶部获取padding值
标签: android layout android-coordinatorlayout