简体   繁体   English

如何在ScrollView内强制线性布局到屏幕底部?

[英]How to force a Linear layout to the bottom of the screen while it's inside a ScrollView?

I found a little difficult to force layout (that is inside a SrollView) to be on the bottom of the screen. 我发现有点难以强制布局(在SrollView内部)位于屏幕的底部。

Well, it would be easy if I detached this Bottom layout off the ScrollView and create it's own layout on the very bottom using Gravity, but when someone expands some of the layouts (or rotate to landscape) the app is going to start using the ScrollView. 好吧,如果我将这个Bottom布局从ScrollView上分离并使用Gravity在最底层创建它自己的布局会很容易,但当有人扩展一些布局(或旋转到横向)时,应用程序将开始使用ScrollView 。 At this moment I want the bottom part to roll up and down with all the stuff and my "Stretching layout" should be only about 8dp (similar to upper dark-blue line) this time. 此时我希望底部用所有东西上下滚动,这次我的“拉伸布局”应该只有大约8dp(类似于上方的深蓝色线)。

在此输入图像描述

Here is my structure: (it is not so complicated how it might seems to be) :) 这是我的结构:(它似乎并不是那么复杂):)

<ScrollView
    android:id="@+id/lt_bcnbldmenu_scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/lt_FillScrl"
    android:scrollbars="vertical" >

    <LinearLayout //HELPER LAYOUT
        android:id="@+id/lt_ScrollingPart"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        android:gravity="bottom"
        android:orientation="vertical" >

        <LinearLayout //only to make some space (upper dark-blue line)
            android:id="@+id/lt_Fill1"
            android:layout_width="fill_parent"
            android:layout_height="6dp"
            android:background="#70000000"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout //blue background layout
            android:id="@+id/lt_bgLine"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/lt_Fill1"
            android:background="@drawable/bg_line"
            android:orientation="vertical" >

            <LinearLayout //MENU LAYOUT
                android:id="@+id/lt_Menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/bg_main"
                android:orientation="vertical" >

                <LinearLayout //Stuff (first layout)
                    android:id="@+id/lt_pic"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="top|fill_horizontal"
                    android:orientation="horizontal" >

                    <ImageButton
                        android:id="@+id/img_bcnbldmenu_pic"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:background="@drawable/bg_main_rd"
                        android:padding="16dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/lighthouse" />

                    <EditText
                        android:id="@+id/edt_bcnbldmenu_adress"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:background="@drawable/bg_main_d"
                        android:ems="10"
                        android:gravity="start"
                        android:hint="Beacon Adress..."
                        android:inputType="textMultiLine"
                        android:padding="16dp"
                        android:textColor="#222222"
                        android:textColorHint="#777777"
                        android:textSize="14sp" />

                </LinearLayout>

                <LinearLayout //Stuff (second layout)
                    android:id="@+id/lt_group"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_main_ud"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/edt_bcnbldmenu_group"
                        android:layout_width="fill_parent"
                        android:layout_height="100dp"
                        android:layout_centerHorizontal="true"
                        android:layout_gravity="left"
                        android:layout_weight="1"
                        android:background="#00000000"
                        android:ems="10"
                        android:gravity="center"
                        android:hint="Group..."
                        android:inputType="textPersonName"
                        android:minHeight="100dp"
                        android:textColor="#222222"
                        android:textColorHint="#777777" />

                    <Button
                        android:id="@+id/btn_bcnbldmenu_list"
                        android:layout_width="68dp"
                        android:layout_height="fill_parent"
                        android:layout_gravity="bottom|right"
                        android:layout_margin="16dp"
                        android:background="@drawable/btn_list"
                        android:gravity="center_vertical|center_horizontal"
                        android:textOff="@string/nothing"
                        android:textOn="@string/nothing" />

                </LinearLayout>

                <EditText //Stuff (third layout - actually only a textbox)
                    android:id="@+id/edt_BcnBldMenu_Description"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#00000000"
                    android:ems="10"
                    android:gravity="top|left"
                    android:hint="Description..."
                    android:inputType="textMultiLine"
                    android:minHeight="100dp"
                    android:padding="16dp"
                    android:textColor="#222222"
                    android:textColorHint="#777777"
                    android:textSize="16sp" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout //STRETCHING LAYOUT (lower dark-blue line)
            android:id="@+id/lt_Fill2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#70000000"
            android:minHeight="32dp"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout //BOTTOM LAYOUT
            android:id="@+id/lt_BottomMenu"
            android:layout_width="fill_parent"
            android:layout_height="62dp"
            android:layout_weight="1"
            android:background="@drawable/bg_line"
            android:gravity="bottom"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_gravity="left"
                android:layout_weight="0"
                android:background="#00000000"
                android:scaleType="center"
                android:src="@drawable/uncheck_press" />

            <ImageView //empty image to make some space here
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:src="#00000000" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="100dp"
                android:layout_height="fill_parent"
                android:layout_gravity="right"
                android:layout_weight="0"
                android:background="#00000000"
                android:scaleType="center"
                android:src="@drawable/crane" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

I hope there is an easy way to figure this out, because otherwise I will have to do it programmatically somehow which I really want to avoid. 我希望有一个简单的方法来解决这个问题,因为否则我将不得不以某种方式编程,我真的想避免。

Thanks in advance! 提前致谢! I am looking for a solution for several hours. 我正在寻找几个小时的解决方案。

Add this property to your ScrollView in XML: 将此属性添加到XML中的ScrollView:

android:fillViewport="true"

This will make the "Helper layout" at least as high as the ScrollView in the case if scrolling is not necessary. 如果不需要滚动,这将使“Helper布局”至少与ScrollView一样高。

The easiest way in my opinion of solving your problem is to check if the helper Linearlayout has less height than the ScrollView. 我认为解决问题的最简单方法是检查帮助程序Linearlayout的高度是否低于ScrollView。

Also note that the preferred xml syntax is "match_parent" and not "fill_parent", which is technically deprecated. 另请注意,首选的xml语法是“match_parent”而不是“fill_parent”,这在技术上已弃用。

1. In the XML change layout_height to wrap_content for the helper Linearlayout, also remove layout_gravity and gravity, useless code for this solution. 1.在XML更改layout_height to wrapper Linearlayout的wrap_content中,同时删除layout_gravity和gravity,此解决方案无用的代码。 You also add an empty view into the helper LinearLayout. 您还可以在帮助程序LinearLayout中添加一个空视图。 Your XML should look like this: 您的XML应如下所示:

<ScrollView
    android:id="@+id/lt_bcnbldmenu_scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/lt_FillScrl"
    android:scrollbars="vertical" >

    <LinearLayout //HELPER LAYOUT
        android:id="@+id/lt_ScrollingPart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/lt_empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <LinearLayout //only to make some space (upper dark-blue line)
        android:id="@+id/lt_Fill1"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:background="#70000000"
        android:orientation="vertical" >
    </LinearLayout>
....

2. in the OnCreate(...) method of the respective Activity, add the following: 2.在相应Activity的OnCreate(...)方法中,添加以下内容:

final ScrollView scrollView = (ScrollView)findViewById(R.id.lt_bcnbldmenu_scroll);
final LinearLayout helperLayout = (LinearLayout)findViewById(R.id.lt_ScrollingPart);
final View emptyView = findViewById(R.id.lt_empty);

// when post(...) is called all View heights have been measured by Android
helperLayout.post(new Runnable() {
    @Override
    public void run() {
        int nHeightDiff = scrollView.getHeight() - helperLayout.getHeight();
        // Does the ScrollView have more height than what it contains?
        if (nHeightDiff > 0) {
            // add padding to compensate for the missing height, you can also set the height directly via LinearLayout.LayoutParams(...)
            emptyView.setPadding(0, nHeightDiff, 0, 0);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM