繁体   English   中英

Android布局,视图固定在底部

[英]Android layout with view fixed to bottom

我正在尝试在Andorid中创建布局。 我需要一个固定在屏幕底部的TextView(下图中的2)。 内容(图中的1)由scrollview和editTexts组成。

我的问题是当软键盘出现时(图中的3),在键盘上方也出现了标记为2的红色框。 我希望红色框保持在屏幕外,并且整个内容在软键盘上方的剩余空间中滚动。

我试过将红色框(2)放在LinearLayout中,但它永远不会固定在底部,在屏幕底部总是有一个小缝隙。 我也尝试过更改清单中的android:windowSoftInputMode,但这不是所需的效果。

这就是我所拥有的:

<?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_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:isScrollContainer="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText />
            <EditText />

        </LinearLayout>
    </ScrollView>

    <TextView
         android:id="@+id/3"
         android:layout_alignParentBottom="true"
         text="3" />
</RelativeLayout>

android布局问题的插图

任何建议将不胜感激。

您需要做的就是将活动的windowSoftInputMode标志切换为“ adjustPan”。 查看官方文档以获取更多信息。

<activity
   ...
   android:windowSoftInputMode="adjustPan"> 
</activity>

尝试使用以下布局,看看这是否是您想要的。

<?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"
    android:layout_centerInParent="true"
    android:isScrollContainer="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <EditText
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:id="@+id/ed1"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"/>
        <EditText
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:id="@+id/ed2"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"/>

    </LinearLayout>
</ScrollView>

<TextView
    android:id="@+id/btext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="this is 3rd view"
    android:layout_marginBottom="20dp"
    android:layout_centerHorizontal="true"/>
</RelativeLayout>

TL; DR版本,我只是在您的scrollview代码中删除了两行:

android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"

  android:layout_centerInParent="true"

看看是否有帮助!

以及为什么在scrollview上同时使用android:layout_alignParentTop =“ true” android:layout_alignParentBottom =“ true”

这是一个可行的想法。

  1. 调整布局,使红色框(2) 位于 LinearLayout内
  2. 在红色框和最后一个EditText之间创建一个小视图
  3. 给红色框和LinearLayout指定一个ID,以便我们可以从java类中对其进行管理。

您的xml应该如下所示:

<?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_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:isScrollContainer="false">

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText />
            <EditText />

            <View
                android:id="@+id/stretcher"
                android:layout_width="match_parent"
                android:layout_height="0dp" />

            <TextView
                android:id="@+id/2"
                android:layout_gravity="bottom"
                text="2" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

现在在您的java onActivityCreated中添加以下代码:

    final View mainLayout = getView();
    final View mainContent = getView().findViewById(R.id.content);
    final View stretcherView = getView().findViewById(R.id.stretcher);

    //Main layout uses weight some, so we can't hard code the size of the circles.
    //We must dynamically re-size
    mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (android.os.Build.VERSION.SDK_INT >= 16) {
                mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            //Calculate the desired height of the stretcher view to be the remainder between full screen and content.
            int stretchHeight = mainLayout.getHeight() - mainContent.getHeight();

            //Apply calculated height remainder to stretched view.
            //This enables our bottom box to be pushed to the bottom without obstructing the content when the keyboard appears.
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) stretcherView.getLayoutParams();
            params.height = stretchHeight;
            stretcherView.setLayoutParams(params);
        }
    });

视图侦听器测量整个视图的高度,然后减去内容的高度。 这使我们在内容和屏幕底部之间留有空隙。 现在,可以使用担架视图将红色框向下推到页面底部,就像它是alignParentBottom一样。 出现键盘时,红色框位于滚动条内部。

暂无
暂无

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

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