【问题标题】:Floating Action Button going below CardView on pre Lollipop浮动动作按钮在棒棒糖前的 CardView 下方
【发布时间】:2016-02-22 14:39:51
【问题描述】:

我正在开发一个安卓应用程序。在其中,有两张卡,并且在这两张卡之间有一张 FAB。屏幕在 API 21 中看起来很完美,但低于 21,按钮的下半部分位于第二张卡片下方。已附加两张图片:PREVIEW BEFORE API 21PREVIEW ON API 21。请帮助。谢谢!

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <View
        android:background="@color/colorAccent"
        android:layout_height="5dip"
        android:layout_gravity="start"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="05dp">

<android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    app:cardUseCompatPadding="true"
    android:layout_marginStart="-4dp"
    android:layout_marginLeft="-4dp"
    android:layout_marginBottom="-4dp"
    android:layout_marginTop="-4dp"
    android:layout_marginRight="-4dp"
    android:layout_marginEnd="-4dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:elevation="500dp"
    app:cardCornerRadius="5dp">

    //First CARDVIEW ELEMENTS 

</android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        android:clickable="true"
        android:onClick="SampleClick"
        android:layout_alignParentRight="true"
        android:src="@mipmap/create_button"
        android:layout_alignBottom="@id/header"
        android:layout_marginBottom="-32dp"
        android:layout_marginRight="20dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@id/header">

        <View
            android:background="@color/colorAccent"
            android:layout_height="5dip"
            android:layout_gravity="start"
            android:layout_width="match_parent" />

        <android.support.v7.widget.CardView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/footer"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            app:cardUseCompatPadding="true"
            android:layout_marginStart="-4dp"
            android:layout_marginLeft="-4dp"
            android:layout_marginBottom="-4dp"
            android:layout_marginTop="-4dp"
            android:layout_marginRight="-4dp"
            android:layout_marginEnd="-4dp"
            android:elevation="500dp"
            app:cardCornerRadius="5dp">

    //SECOND CARDVIEW ELEMENTS           

            </android.support.v7.widget.CardView>
        </LinearLayout>
    </RelativeLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

【问题讨论】:

  • 我给你例子请看一下

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


【解决方案1】:

您可以通过将layout_anchor 标签添加到您的 FloatingActionButton 来实现这一点,这意味着在此视图上绘制我的浮动,这将需要您将顶部布局也更改为 CoordinatorLayout

请看一下

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" // add this line
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    app:cardUseCompatPadding="true"
    android:layout_marginStart="-4dp"
    android:layout_marginLeft="-4dp"
    android:layout_marginBottom="-4dp"
    android:layout_marginTop="-4dp"
    android:layout_marginRight="-4dp"
    android:layout_marginEnd="-4dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:elevation="500dp"
    app:cardCornerRadius="5dp">

    //First CARDVIEW ELEMENTS 

</android.support.v7.widget.CardView>
    <android.support.design.widget.FloatingActionButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom|right"
              android:layout_margin="16dp"
              android:src="@drawable/ic_done"
              app:layout_anchor="@id/header" // whatever you want
              app:layout_anchorGravity="bottom|right|end" />


<android.support.v7.widget.CardView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/footer"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            app:cardUseCompatPadding="true"
            android:layout_marginStart="-4dp"
            android:layout_marginLeft="-4dp"
            android:layout_marginBottom="-4dp"
            android:layout_marginTop="-4dp"
            android:layout_marginRight="-4dp"
            android:layout_marginEnd="-4dp"
            android:elevation="500dp"
            app:cardCornerRadius="5dp">

    //SECOND CARDVIEW ELEMENTS           

            </android.support.v7.widget.CardView>


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2016-02-03
    • 2015-12-13
    • 2016-05-30
    • 2016-10-09
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多