为了账号安全,请及时绑定邮箱和手机立即绑定

修复内容小的时候在屏幕底部的按钮,如果内容大到可以占据整个屏幕,则在内容的底部

修复内容小的时候在屏幕底部的按钮,如果内容大到可以占据整个屏幕,则在内容的底部

白衣非少年 2021-11-03 10:46:12
为此,我正在将 android studio 与 java 一起使用。问题说明 修复内容小的时候在屏幕底部的按钮,如果内容大到可以占据整个屏幕,则在内容的底部。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 文件实现这两种方案,但是当内容很小时使用第二种实现,然后按钮位于内容下方而不是屏幕底部。
查看完整描述

1 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

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>


查看完整回答
反对 回复 2021-11-03
  • 1 回答
  • 0 关注
  • 170 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信