在CardView和RecyclerView上添加浮动按钮

时间:2017-08-28 04:50:16

标签: android android-recyclerview material-design android-cardview androiddesignsupport

我有这个问题来容纳我的浮动按钮,我不明白为什么它不起作用。 我很感激你的回答,因为我找不到发生这种情况的原因,滚动或重新加载片段时的一个细节将容纳一些浮动按钮。 对不起我的英语。

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="wrap_content"
android:background="@color/colorBackground"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.CardView
        android:id="@+id/mRequestCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:elevation="100dp"
        android:orientation="horizontal"
        app:cardBackgroundColor="@color/colorWhite"
        app:cardCornerRadius="3dp">

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

            <View
                android:id="@+id/vDivisor"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorGreen" />
                   ...

        </RelativeLayout>

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

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


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginRight="@dimen/fab_margin"
    android:backgroundTint="@color/colorPurple"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_route_direction"
    app:layout_anchor="@id/mRequestCard"
    app:layout_anchorGravity="right|end|bottom" />

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

这是我的RecyclerView,它在片段中膨胀

<FrameLayout 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"
tools:context="com.test.HistorialFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecyclerRequest"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>

这是我的片段,在那里我展示了RecyclerView

<FrameLayout 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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.test.MainActivity"
tools:showIn="@layout/app_bar_main">
</FrameLayout>

3 个答案:

答案 0 :(得分:0)

你应该像这样尝试RelativeLayout

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:backgroundTint="@color/colorBlack"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="@dimen/activity_corner"
        android:layout_marginBottom="@dimen/activity_margin">
        <!--your cardview item here-->
    </android.support.v7.widget.CardView>
</RelativeLayout>

代表您的RecyclerView商品

答案 1 :(得分:0)

检查这是否对您有所帮助:

private handleError(error: any) {
    let description = 'There was an error: ' + error.status;
    let errors = {
        errorcode: error.status,
        errorstatus: error.statusText,
        errordescription: description
    };
    return errors;
}

答案 2 :(得分:0)

尝试使用RelativeLayout并使您的FloatingActionButton 属性 android:layout_alignParentBottom="true"         android:layout_alignParentRight="true"如下面的代码

<RelativeLayout android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="horizontal"
 xmlns:android="http://schemas.android.com/apk/res/android">

 <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:backgroundTint="@color/colorBlack"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"/>
 <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardCornerRadius="@dimen/activity_corner"
    android:layout_marginBottom="@dimen/activity_margin">


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

</RelativeLayout>