如何在卡片视图的中下方中心放置视图?

时间:2016-05-30 14:18:24

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

到目前为止我有这个布局:

enter image description here

<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="150dp">

<android.support.v7.widget.CardView
    android:id="@+id/viewA"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.CardView> 

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    app:layout_anchor="@id/viewA"
    app:layout_anchorGravity="bottom|center"/>

如何将按钮放在协调器布局的中下(宽度)并得到这样的结果? :

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该在linearlayout中使用CardView并在FAB的属性app:layout_anchor中使用它,如下所示:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="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=".vistas.fragments.FragmentDatosTab">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="350dp"
        android:layout_height="500dp"
        android:layout_gravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="5dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

        </LinearLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/bt_iniciar_sesion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_arrow_forward_white_24dp"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchor="@id/linearlayout"
        app:layout_anchorGravity="center_horizontal|bottom" />

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

结果如下:

FAB in middle-bottom of cardview