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

为此,我正在将 android studio 与 java 一起使用。
问题说明 修复内容小的时候在屏幕底部的按钮,如果内容大到可以占据整个屏幕,则在内容的底部。

1.当内容较小
时,当内容足够小以占据整个屏幕时,我想在屏幕底部固定两个按钮。

http://img3.mukewang.com/6181f80800013bb807281207.jpg

2.当内容很大
时当内容足够大到占据整个屏幕时,它应该出现在整个内容的底部。当用户向下滚动时,按钮应该出现。在这张图片中,如果我想点击按钮,我已经完全向下滚动了。

http://img3.mukewang.com/6181f824000105a307281211.jpg

这是第二个实现的代码


<?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 文件实现这两种方案,但是当内容很小时使用第二种实现,然后按钮位于内容下方而不是屏幕底部。


白衣非少年
浏览 171回答 1
1回答

阿波罗的战车

minHeight 属性是回答这个问题的关键。我首先得到应用程序屏幕大小,然后从中减去按钮和工具栏大小。然后我将其设置为我的内容所在的线性布局的最小尺寸。我使用了 onWindowFocusChanged 函数,因为有时布局可能需要时间才能完全加载,因此获取按钮高度可能会失败。@Overridepublic void onWindowFocusChanged(boolean hasFocus) {&nbsp; &nbsp; super.onWindowFocusChanged(hasFocus);&nbsp; &nbsp; Configuration configuration = getResources().getConfiguration();//returns the app screen size.&nbsp; &nbsp; int screenHeightDp = configuration.screenHeightDp-56;// 56 is the max height of the android toolbar.&nbsp;&nbsp; &nbsp; float density = this.getResources().getDisplayMetrics().density;&nbsp;&nbsp; &nbsp; screenHeightDp = (int)(screenHeightDp*density);//changes the dp to pixel&nbsp; &nbsp; Button perWordHistory = findViewById(R.id.perWordHistory);&nbsp; &nbsp; int heightOfButton = perWordHistory.getHeight()*2; // as I have two buttons&nbsp; &nbsp; screenHeightDp = screenHeightDp - heightOfButton;&nbsp; &nbsp; LinearLayout linearLayout = findViewById(R.id.layoutContainingRecycleView);&nbsp; &nbsp; linearLayout.setMinimumHeight(screenHeightDp);}这是经过一些更改后的布局代码。<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent">&nbsp; &nbsp; <ScrollView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content">&nbsp; &nbsp; &nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; &nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/layoutContainingRecycleView">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <android.support.v7.widget.RecyclerView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/eachWord"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="8dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:scrollbars="vertical" />&nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; &nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/perWordHistory"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/title_history" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/Help"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/help" />&nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; </ScrollView></RelativeLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java