锁定卡片视图上的浮动按钮

问题描述:

我试图在卡片视图上锁定浮动按钮而没有成功。锁定卡片视图上的浮动按钮

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


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="8dp" 
     android:background="@android:color/white" 
     card_view:cardCornerRadius="2dp" 
     card_view:cardElevation="2dp"> 


      ..... my view ... 


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

    <android.support.design.widget.FloatingActionButton 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     app:layout_anchor="@id/card_view" 
     app:layout_anchorGravity="bottom|end|right" 
     android:layout_margin="16dp" 
     android:clickable="true"/> 

</LinearLayout> 

的结果是这样的:

我想要的浮动按钮出现像绿色圆圈。 此布局用于RecyclerView

这是一种实现此目的的方法吗?

+0

只需在FloatingActionButton android:layout_gravity =“center | bottom | right”中加入这一行,如果它不起作用,我会帮到 – Vadivel

您能否在下面的代码中添加您的浮动按钮

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:orientation="vertical" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="297dp" 
    android:id="@+id/relativeLayout"> 


    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="8dp" 

     android:background="@android:color/white" 
     card_view:cardCornerRadius="2dp" 
     card_view:cardElevation="2dp"> 


     ..... my view ... 


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

</RelativeLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    app:srcCompat="@android:drawable/ic_dialog_email" 
    android:layout_marginRight="12dp" 
    android:layout_marginEnd="12dp" 
    android:layout_marginTop="165dp" 
    android:layout_alignTop="@+id/relativeLayout" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

+0

它没有我右边的浮动按钮,但没有像绿色圆圈一样锚定在卡式视图边框上的技巧。 – WhatsUp

+0

我更新mu后,那个代码 – Vadivel

发布提问DOA咬了谷歌上搜索,在第二个结果之前,这answer

编辑

我实现了这个在一个单独的项目,它工作正常...

<?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:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <android.support.v7.widget.CardView 
     android:id="@+id/cv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up." /> 

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

</android.support.v4.widget.NestedScrollView> 

<android.support.design.widget.FloatingActionButton 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    app:layout_anchor="@id/cv" 
    app:layout_anchorGravity="right|end|bottom" 
    android:layout_marginRight="32dp" 
    android:clickable="true"/> 

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

我先读这个不行...... – WhatsUp

+0

你得错过一些东西,这个代码的行为就像你需要的一样,我们期望 –

将整个布局包装在CoordinatorLayout中,并将您的锚点定义到卡片视图。

app:layout_anchor="@id/card_view" 
app:layout_anchorGravity="bottom|end|right" 

然后它应该工作。