【问题标题】:Use Button instead of FloatingActionButton with AppBarLayout anchor in CoordinatorLayout在 CoordinatorLayout 中使用带有 AppBarLayout 锚点的 Button 而不是 FloatingActionButton
【发布时间】:2018-08-18 06:05:06
【问题描述】:

我在使用普通 Button 并将 AppBarLayout 设置为该 Button 的锚点时遇到问题,因为 Button 的上部放置在 AppBarLayout 下。它在使用FloatingActionButton 时有效,我尝试设置高度并使用bringToFront,但没有任何效果。

<?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.support.design.widget.AppBarLayout
        android:id="@+id/materialup.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        ​​​
        <android.support.v7.widget.Toolbar
            android:id="@+id/materialup.toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="enterAlways|snap"
            style="@style/ToolBarWithNavigationBack"
            />
        ​
        <LinearLayout
            android:id="@+id/materialup.title_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="30dp"
            android:orientation="vertical"
            android:paddingTop="8dp"
            android:gravity="center"
            app:layout_scrollFlags="scroll|enterAlways|snap">
            ​
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:text="@string/title_mup_name"
                />
            ​
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
                android:text="@string/subtitle_mup"
                android:textColor="@android:color/white"
                />
            ​
        </LinearLayout>
        ​
    </android.support.design.widget.AppBarLayout>
    ​
    <android.support.v4.view.ViewPager
        android:id="@+id/materialup.viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
    ​

    <Button
        android:id="@+id/btn_submit_trip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@+id/materialup.appbar"
        app:layout_anchorGravity="bottom|center_horizontal"
        android:text="Submit"
        />

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

【问题讨论】:

  • 在向上滚动时,您是否希望您的按钮与您的 appBar 一起向上移动?还是 appBar 固定不可滚动??
  • @SantanuSur 是的,所有这些都已经作为协调器布局的一部分工作了,唯一不工作的是按钮显示为一半,但它可以滚动并正确运行
  • 尝试将协调器布局包裹在约束布局内,然后将按钮保留在约束布局内
  • 好问题顺便说一句 ..:) 希望我现在在 pc ryt 前面 .. 会喜欢解决它 ..
  • 我确定它与行为有关,但我仍然无法弄清楚。如果我要使用 ConstraintLayout,我将失去现在的滚动行为!

标签: android android-coordinatorlayout floating-action-button


【解决方案1】:

将您的提交按钮包裹在 Cardview 中,并将高度设置为 cardview。它会起作用的。我使用 textview 而不是按钮:

<androidx.cardview.widget.CardView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:cardElevation="4dp"
    app:layout_anchor="@+id/materialup.appbar"
    app:layout_anchorGravity="bottom|center_horizontal">

    <TextView
        android:id="@+id/btn_submit_trip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="12dp"
        android:text="Submit"
        android:textStyle="bold"/>
</androidx.cardview.widget.CardView>

【讨论】:

    【解决方案2】:

    您可以尝试两种可能的解决方案:首先尝试从应用栏中的线性布局中删除 padding_bottom,同样您将最小化应用栏的表面以让按钮出现。 第二个添加 android:layout_marginTop 到你的按钮。 让我知道这是否有效,否则我们可以搜索其他选项。

    【讨论】:

    • 感谢您的回答,这行不通,因为我需要把它放在这个地方
    • 你能试着给按钮分配这个吗:android:layout_weight="1"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2016-02-13
    • 2020-11-16
    • 2015-12-15
    相关资源
    最近更新 更多