android - 显示小吃店时Android向上移动按钮
问题描述
我想在显示小吃店时向上移动按钮,这样就不会隐藏它们。我已经尝试过堆栈溢出的不同解决方案,但我无法找到解决方案。这是我的布局文件:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp" >
<include
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/fragment_expiry_dates_list_header" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/products_list"
android:name="com.example.expirydate.ui.main.ExpiryDatesFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header"
android:layout_above="@id/buttonNewOrOpened"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="3dp"
app:layoutManager="LinearLayoutManager"
tools:context=".expirydatefragment.ExpiryDatesFragment"
tools:listitem="@layout/fragment_expiry_dates_list_item" />
<Button
android:id="@+id/buttonNewOrOpened"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="false"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:enabled="false"
android:text="New" />
<Button
android:id="@+id/buttonUsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@id/buttonNewOrOpened"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:enabled="false"
android:text="Used" />
<ImageButton
android:id="@+id/buttonScanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_toStartOf="@id/buttonAdd"
android:background="@drawable/roundcorner"
android:padding="8dp"
android:src="@android:drawable/ic_menu_gallery"
android:contentDescription="scan product" />
<ImageButton
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:padding="8dp"
android:background="@drawable/roundcorner"
android:src="@android:drawable/ic_input_add"
android:contentDescription="add product" />
</RelativeLayout>
我想做的是移动小吃店上方底部的那 4 个按钮。显示小吃店的代码:
Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "msg", Snackbar.LENGTH_SHORT);
snackbar.setAction("Undo", v -> {});
snackbar.show();
我尝试了几种解决方案,添加了我自己的 layout_behavior 或添加了 app:insetEdges="bottom",但没有任何效果。
解决方案
尝试使用app:insetEdges="bottom"
. 但是首先insetEdges
并且layout_dodgeInsetEdges
只能与CoordinatorLayout
. 您需要将您的更改RelativeLayout
为CoordinatorLayout
. 之后将您的按钮分组到一个布局中,例如,它可以添加和RelativeLayout
添加到它。由于 Snackbar 默认情况下,您只需将该属性设置为按钮容器。app:layout_dodgeInsetEdges="bottom"
android:layout_gravity="bottom"
app:insetEdges="bottom"
为了使 CoordinatorLayout 正常工作,还假设您header
的包装在AppBarLayout
并添加app:layout_behavior="@string/appbar_scrolling_view_behavior"
到RecyclerView
. 在这里,我可以提供此解决方案的模板。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Your header here or your header need to be already wrapped in AppBarLayout -->
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/products_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
tools:context=".expirydatefragment.ExpiryDatesFragment"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<RelativeLayout
android:id="@+id/buttonsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_dodgeInsetEdges="bottom"
android:layout_gravity="bottom">
<!--Your Buttons here-->
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
推荐阅读
- math - 如何在 3d 空间中旋转矢量?坐标系不是全局 xyz。我有坐标系每个轴的向量
- math - 给定一个排名的人员列表,我怎样才能有效地将他们分成 2 个平衡的团队?
- thrift - 为什么apache thrifts 传输层分为低级传输和传输包装器
- python - 输入列表中不存在中值?
- java - org.springframework.jms.IllegalStateException:会话已关闭
- mysql - 如果我向某人或某人向我发送消息,我想显示不同用户的信使等名称,按 id desc 排序
- ios - 具有大量图像的 UITableView
- javascript - 在单个文件中导入所有 vue js 组件
- ios - ios背景白色渐变
- tableau-api - 如何从相对日期过滤器中获取天数?