【问题标题】:Positioning views in Coordinator Layout在协调器布局中定位视图
【发布时间】: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


【解决方案1】:

使用这 3 个属性相互定位

android:layout_gravity=""
app:layout_anchorGravity=""
app:layout_anchor="@id/xxxx"

这里还有一个完整的xml可以帮助你

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".home.HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        android:id="@+id/app_barlayout"
        android:theme="@style/AppTheme.AppBarOverlay">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collaps_toolabar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle">
            <LinearLayout
                android:id="@+id/layout_currentMatch_item"
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:orientation="vertical">


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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_anchorGravity="bottom"
        app:layout_anchor="@id/app_barlayout"
        android:orientation="horizontal" />

   

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【讨论】:

    【解决方案2】:

    您可以在 CoordinatorLayout 中使用 LinearLayout 来安排您的视图,如果它符合您的需要或 ConstraintLayout


    我希望这个决定的依据是明确的。看看这个例子:

    <?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"
            tools:context=".MainActivity">
    
        <android.support.design.widget.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.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"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <include
                    layout="@layout/content_main"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
            <LinearLayout
                    android:id="@+id/bar"
                    android:orientation="vertical"
                    android:layout_weight="0"
                    android:background="@color/colorAccent"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"/>
        </LinearLayout>
    
        <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                app:srcCompat="@android:drawable/ic_dialog_email"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 感谢您的建议。这是一个可行的解决方案,但它不使用底部应用栏,并且失去了您使用它获得的良好效果 :) 我可能会在滚动时隐藏底部栏以避免这个问题。
    • @Jeremi 什么时候应该显示底栏?仅当您完全向下滚动列表时?
    • 还是底部栏的行为类似于小吃店?
    • 我想一直显示底栏,只在滚动时隐藏标签布局。
    • 我只是设法在向下滚动时隐藏了底部的应用栏,并在向上滚动时再次显示它:)它具有非常令人愉悦的效果,只需要添加 bar.hideOnScroll = true 我会说会很高兴添加一些额外的底部填充以防万一列表中没有足够的项目滚动,否则,某些项目可能仍然不可见。
    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2016-08-09
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多