java - 内容小时将按钮固定在屏幕底部,如果内容大到足以占据整个屏幕,则将按钮固定在内容底部
问题描述
为此,我正在使用带有 java 的 android studio。
问题说明
当内容较小时,将按钮固定在屏幕底部,如果内容大到足以占据整个屏幕,则固定在内容底部。
1.当内容很小的时候,
我想在内容小到占据整个屏幕的时候,在屏幕底部固定两个按钮。
2. 当内容很大
时当内容大到足以占据整个屏幕时,它应该出现在整个内容的底部。当用户向下滚动时,按钮应该出现。就像在这张图片中,如果我想点击按钮,我已经完全向下滚动了。
这是第二个实现的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/eachWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:scrollbars="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/perWordHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="History" />
<Button
android:id="@+id/perWordBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我想使用一个 xml 文件来实现这两种场景,但在内容很小的情况下使用第二种实现,然后按钮位于内容下方而不是屏幕底部。
解决方案
minHeight 属性是回答这个问题的关键。
我首先得到了应用程序的屏幕大小,然后从中减去了按钮和工具栏的大小。然后我将其设置为我的内容所在的线性布局的最小大小。
我使用了 onWindowFocusChanged 函数,因为有时布局可能需要时间才能完全加载,因此获取按钮高度可能会失败。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Configuration configuration = getResources().getConfiguration();//returns the app screen size.
int screenHeightDp = configuration.screenHeightDp-56;// 56 is the max height of the android toolbar.
float density = this.getResources().getDisplayMetrics().density;
screenHeightDp = (int)(screenHeightDp*density);//changes the dp to pixel
Button perWordHistory = findViewById(R.id.perWordHistory);
int heightOfButton = perWordHistory.getHeight()*2; // as I have two buttons
screenHeightDp = screenHeightDp - heightOfButton;
LinearLayout linearLayout = findViewById(R.id.layoutContainingRecycleView);
linearLayout.setMinimumHeight(screenHeightDp);
}
这是经过一些更改后的布局代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutContainingRecycleView">
<android.support.v7.widget.RecyclerView
android:id="@+id/eachWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/perWordHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_history" />
<Button
android:id="@+id/Help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/help" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
推荐阅读
- curl - 如何从 Bitbucket repo 获取提交 sha 的流/列表
- ios - 显示弹出控制器时,如何让手势识别器在基本控制器上工作?
- node.js - 如何在 Mongoose 使用 Joi 更新“空”数据
- arcore - 适用于小米 mi 9 的 Arcore
- python - 在 Python 中查找相对于项目根目录的文件
- java - 带有千位分隔符 0 值的 DecimalFormatter 返回 .00
- terraform - 你如何为 IAC 设置你的仓库?
- node.js - 我必须为迁移回滚编写反向操作吗?
- php - 如何通过 php 使用 sendgrid 正确发送电子邮件
- c# - C# Windows 窗体应用程序 ListView:如何强制 ListView 不更新?