当键盘打开时,带有片段的活动不会调整大小 [英] Activity with fragments does not resize when the keyboard opens

查看:26
本文介绍了当键盘打开时,带有片段的活动不会调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动中有一个 RelativeLayout 有 2 个孩子:

  • 作为背景的ImageView
  • 具有 2 个片段容器的 LinearLayout

activity_main.xml

<线性布局android:id="@+id/ContainerA";android:layout_width=match_parent"android:layout_height="wrap_content";android:layout_gravity=中心"机器人:方向=垂直"/><线性布局android:id="@+id/ContainerB";android:layout_width=match_parent"android:layout_height=match_parent"android:background="@color/background_gray";机器人:方向=垂直"/></LinearLayout></RelativeLayout>

容器 A 只是显示一个不会改变的片段.
然而,容器 B 持有一个片段,它会根据用户点击而改变.

容器 B 有两种变化:

  • 它可以扩展(从大约 70% 的高度到全高).
  • 它改变了片段.

Container B 可以容纳的片段之一是一个表单,其中包含 EditText 和其他视图.

我的问题是当键盘打开时我无法调整活动的大小.
我在清单文件中设置了 adjustResize 并且我的主题不是全屏主题(这显然是导致此问题的原因).我也尝试编辑视图,添加 ScrollViews 但绝对没有任何效果.

我用于此活动的主题是来自 AppCompat 库的带有深色操作栏的浅色主题.我只编辑了主题以允许覆盖操作栏.这不是的问题,因为我已经尝试删除这些编辑无济于事.

这是表单布局的链接

TL;DR adjustResize/adjustPan 对我不起作用.搜索,尝试了各种解决方案,没有任何效果.我的问题是;在我的情况下,这是什么原因?

更新:AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android";...><uses-sdk android:minSdkVersion="11";android:targetSdkVersion=19"/><application ... android:theme="@style/AppTheme"><activity android:name=".SplashScreenActivity";android:label="@string/app_name";android:screenOrientation="肖像"android:theme="@android:style/Theme.Black.NoTitleBar"><意图过滤器><action android:name="android.intent.action.MAIN";/><category android:name="android.intent.category.LAUNCHER";/></意图过滤器></活动><activity android:name=".MainActivity";android:configChanges="keyboardHidden|screenSize"android:label="@string/app_name";android:screenOrientation="肖像"android:theme="@style/OverlayActionBarTheme";android:windowSoftInputMode="adjustResize";/><activity android:name=".SettingsActivity";android:label="@string/title_activity_settings";android:parentActivityName=.MainActivity"android:windowSoftInputMode="adjustResize"><meta-data android:name="android.support.PARENT_ACTIVITY";android:value=.MainActivity"/></活动></应用程序></清单>

更新 2:
我创建了一个全新的应用程序,将我的表单布局添加为主要布局,并测试问题是否出在布局本身上.事实证明是这样,因为即使在这个新创建的应用程序上,它似乎也没有调整大小.

如上所述,布局的链接可以在这里

更新 3:我设法通过用 ScrollView 包围整个布局,使其在新应用程序上运行.由于某种原因,我无法在原始应用中复制相同的效果……仍在尝试找出原因.

解决方案

当软键盘出现在屏幕上时,应用程序 UI 的可用空间就会减少.系统决定如何组织应用程序的 UI 和软键盘之间的可用空间.

  • 如果窗口内容包含ListViewScrollView,只要所有内容都可见,应用程序的窗口就会调整大小.
  • 如果重新调整大小不可行,则使用平移和扫描方法,该方法仅涉及滚动应用程序的窗口,以便当前聚焦的视图可见.

由于您的布局不包含任何ListViewScrollView,因此排除了重新调整大小的可能性.

窗口的根视图是您最初添加 LinearLayoutFrameLayout.由于LinearLayout 不支持滚动,所以也排除了pan and scan 方法.因此,将布局包装在 ScrollView 中解决了滚动问题.

您可以参考 Android 开发者博客 了解更多详情.

更新 1:

OP 能够解决该问题,如本答案所示.该问题是由于一个片段在动画后覆盖另一个片段而发生的,而这些 Fragment 的父对象是 LinearLayout.为了叠加目的,您只需要使用 RelativeLayoutFrameLayout.

In my main activity there is a RelativeLayout that has 2 childs:

  • An ImageView which serves as the background
  • A LinearLayout that has 2 fragment containers

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    tools:context="${packageName}.${activityClass}">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:baselineAlignBottom="false"
        android:scaleType="fitStart"
        android:src="@drawable/bg_image" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="?android:attr/actionBarSize"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:id="@+id/ContainerA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" />
        
        <LinearLayout
            android:id="@+id/ContainerB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_gray"
            android:orientation="vertical" />

    </LinearLayout>
</RelativeLayout>

Container A simply shows a fragment that doesn't change.
However, Container B holds a fragment that does change depending on user clicks.

Container B changes in 2 ways:

  • It can expand (from a height of about 70% to full height).
  • It changes fragments.

One of the fragments Container B can hold is a form, which contains EditTexts and other views.

My problem is that I can't get the activity to resize when the keyboard is open.
I set adjustResize in the manifest file and my theme is not a fullscreen theme (it is apparently a cause for this problem). I also tried to edit the views, add ScrollViews but absolutely nothing worked.

The theme I'm using for this activity is the light theme with the dark actionbar from the AppCompat library. I only edited the theme to allow an overlay actionbar. This is not the problem as I've already tried removing those edits to no avail.

Here's a link to the form layout

TL;DR adjustResize / adjustPan do not work for me. Searched, tried various solutions, nothing worked. My question is; what is the cause for this in my case?

Update: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" />

    <application ... android:theme="@style/AppTheme">
        <activity android:name=".SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"
            android:configChanges="keyboardHidden|screenSize" android:label="@string/app_name"
            android:screenOrientation="portrait" android:theme="@style/OverlayActionBarTheme"
            android:windowSoftInputMode="adjustResize"  />
        <activity android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity"
            android:windowSoftInputMode="adjustResize">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
    </application>

</manifest>

Update 2:
I created a completely new app, added my form layout as the main layout, and tested whether the problem was in the layout itself. Turns out it is, as even on this newly created app it doesn't seem to resize.

As stated above, the link to the layout can be found Here

Update 3: I managed to make it work on the new application by surrounding the whole layout with a ScrollView. I couldn't replicate the same effect in the original app for some reason... still trying to figure out why.

解决方案

When the soft keyboard appears on the screen, the amount of space available for the application's UI reduces. The system makes decision on how to organize the available space between the application's UI and soft keyboard.

  • If the window content contains ListView, ScrollView, the application's window is resized provided all the content is visible.
  • If re-sizing is not feasible, pan and scan approach is used, which simply involves scrolling the application's window so that the currently focused view is visible.

Since your layout does not contain any ListView, ScrollView, re-sizing is ruled out.

The window's root view is a FrameLayout to which you were originally adding LinearLayout. Since LinearLayout does not support scrolling, pan and scan approach is also ruled out. Hence wrapping the layout inside ScrollView solves the scrolling issue.

You can refer Android Developers blog for more details.

Update 1:

OP was able to solve the issue, as indicated in this answer. The issue was happening due to one fragment overlaying another fragment after animation and the parent of these Fragment's was a LinearLayout. For overlay purpose, you need to use RelativeLayout or FrameLayout only.

这篇关于当键盘打开时,带有片段的活动不会调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆