java - 如何在 RecyclerView 中添加 FloatingActionButton
问题描述
我是 android 编程的新手,我遇到了一个棘手的问题,我花了几个小时试图解决它,但没有成功,我正在寻找如何在 RecyclerView 中添加 FloatingActionButton 我的模板看起来像:
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/messages"
android:layout_width="match_parent"
android:layout_height="451dp"
android:layout_weight="1"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<!-- TODO: fix this by adding the floating button inside the recyclerView -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_launcher"
android:layout_margin="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:gravity="center_vertical">
<EditText
android:id="@+id/message_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/prompt_message"
android:imeActionId="@+id/send"
android:imeActionLabel="@string/action_send"
android:imeOptions="actionSend"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"/>
<ImageButton
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_send"
android:contentDescription="@string/action_send"/>
</LinearLayout>
这是我需要的目标布局:
解决方案
您需要使用相对布局而不是线性布局。查看我的 floatingActionButton 代码,这将对您有所帮助。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@layout/app_toolbar"
android:layout_weight="0.6"
android:orientation="vertical"
android:stateListAnimator="@null">
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_asset_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:background="@android:color/transparent"
android:backgroundTint="@color/white"
android:src="@drawable/icon_camera"
app:rippleColor="@color/white" />
<android.support.v7.widget.RecyclerView
android:id="@+id/messages"
android:layout_width="match_parent"
android:layout_height="451dp"
android:layout_weight="1"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<RelativeLayout/>
推荐阅读
- javascript - Object.entities 过滤器
- angular-material - 使用 AngularMaterial + cdkDrag 将位置重置为原始位置
- rxjs - 有没有更好的方法在第一个完成后调用 api?
- excel - 删除 Excel 中 MATCH 函数标识的行号
- java - 为什么Java代码的输出在“adb”下面,而不是“abd”?
- jquery - 使用 JQuery 显示一个文章元素并同时隐藏其他文章元素
- http - 如果没有连接,Node-Red“http 请求”节点卡在请求中
- mysql - 尽管 where 子句中的列上存在索引,但活动记录查找间歇性变慢
- three.js - Three.js:如何将 3D JSON 模型添加到立方体网格的一个面上
- bash - 使用 bash 脚本从字符串中提取版本