Android 当键盘打开时,带有片段的活动不会调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25303285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Activity with fragments does not resize when the keyboard opens
提问by Asaf
In my main activity there is a RelativeLayout
that has 2 childs:
在我的主要活动中,有一个RelativeLayout
有 2 个孩子的:
- An
ImageView
which serves as the background - A
LinearLayout
that has 2 fragment containers
- 一个
ImageView
其用作背景 - A
LinearLayout
有 2 个片段容器
activity_main.xml
活动_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.
容器 A 只是显示一个不会改变的片段。
然而,容器 B 持有一个片段,它会根据用户点击而改变。
Container B changes in 2 ways:
容器 B 有两种变化:
- It can expand (from a height of about 70% to full height).
- It changes fragments.
- 它可以扩展(从大约 70% 的高度到全高)。
- 它改变了片段。
One of the fragments Container B can hold is a form, which contains EditText
s and other views.
Container B 可以容纳的片段之一是一个表单,其中包含EditText
s 和其他视图。
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.
我的问题是当键盘打开时我无法调整活动的大小。
我adjustResize
在清单文件中设置了我的主题不是全屏主题(这显然是导致此问题的原因)。我也尝试编辑视图,添加ScrollViews
但绝对没有任何效果。
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 notthe problem as I've already tried removing those edits to no avail.
我在此活动中使用的主题是带有AppCompat
库中深色操作栏的浅色主题。我只编辑了主题以允许覆盖操作栏。这不是问题,因为我已经尝试删除这些编辑无济于事。
Here's a link to the form layout
TL;DRadjustResize
/ adjustPan
do not work for me. Searched, tried various solutions, nothing worked. My question is; what is the cause for this in my case?
TL; DRadjustResize
/adjustPan
不适合我。搜索,尝试了各种解决方案,没有任何效果。我的问题是;在我的情况下,这是什么原因?
Update: AndroidManifest.xml
更新: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.
更新2:
我创建了一个全新的应用程序,添加了我的表单布局作为主布局,并测试了问题是否出在布局本身上。事实证明,即使在这个新创建的应用程序上,它似乎也没有调整大小。
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.
更新 3:我设法通过用ScrollView
. 由于某种原因,我无法在原始应用程序中复制相同的效果......仍在试图找出原因。
回答by Manish Mulimani
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.
当软键盘出现在屏幕上时,应用程序 UI 的可用空间就会减少。系统决定如何组织应用程序的 UI 和软键盘之间的可用空间。
- 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.
- 如果窗口内容包含
ListView
,ScrollView
,则只要所有内容都可见,应用程序的窗口将被调整大小。 - 如果重新调整大小不可行,
pan and scan
则使用方法,该方法仅涉及滚动应用程序的窗口,以便当前聚焦的视图可见。
Since your layout does not contain any ListView
, ScrollView
, re-sizing is ruled out.
由于您的布局不包含任何ListView
, ScrollView
,因此排除了重新调整大小的可能性。
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.
窗口的根视图是FrameLayout
您最初添加的LinearLayout
. 由于LinearLayout
不支持滚动,pan and scan
所以也排除了方法。因此,将布局包装在 ScrollView 中解决了滚动问题。
You can refer Android Developers blogfor more details.
您可以参考Android 开发者博客了解更多详情。
Update 1:
更新 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.
OP 能够解决这个问题,如本答案所示。该问题是由于一个片段在动画后覆盖另一个片段而发生的,而这些片段的父对象Fragment
是LinearLayout
. 出于叠加目的,您需要使用RelativeLayout
或FrameLayout
仅。
回答by Ramkailash
You can use this code in your Fragment also
您也可以在 Fragment 中使用此代码
This code provide help for resize view
此代码为调整视图大小提供帮助
public static class MyDialog extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Dialog layout inflater code
// getDialog() need to be called only after onCreateDialog(), which is invoked between onCreate() and onCreateView().
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// return view code
}
}
回答by Vaishali Sutariya
To change the mode to “resize” at the following statement to your activity in “AndroidManifest.xml”.
在“AndroidManifest.xml”中的活动的以下语句中将模式更改为“调整大小”。
<activity android:name=".ActivityName" android:windowSoftInputMode="adjustResize">
<activity android:name=".ActivityName" android:windowSoftInputMode="adjustResize">
and please remove the complexity of LinearLayout ( means you are using multiple level of LinearLayout ) instead of that you can use RelativeLayout also .. using this you can easily set Layout as you want .. try it that also hope your problem may rectifier
并且请删除 LinearLayout 的复杂性(意味着您使用的是多级 LinearLayout )而不是您也可以使用 RelativeLayout .. 使用它您可以根据需要轻松设置 Layout .. 尝试它也希望您的问题可以整流
I hope this helps.
我希望这有帮助。
回答by Asaf
Turns out my problem was caused by a completely unexpected source.
原来我的问题是由一个完全出乎意料的来源引起的。
What happened is that I used a LinearLayout
as the parent of my two fragment containers.
Then, in my expanding animation I raised the bottom container to overlap the other one.
发生的事情是我使用 aLinearLayout
作为我的两个片段容器的父级。
然后,在我的扩展动画中,我抬起底部容器与另一个容器重叠。
I don't really know whybut since LinearLayout
is not supposed to hold overlapping children, switching to a RelativeLayout
or a FrameLayout
immediately solved the issue.
我真的不知道为什么但是因为LinearLayout
不应该容纳重叠的孩子,所以切换到 aRelativeLayout
或 aFrameLayout
立即解决了这个问题。
Since this cause might not be apparent from the code I posted, I do want to apologize for the lack of information, and will credit the most detailed answer as the best one.
由于这个原因可能不会从我发布的代码中明显看出,我确实想为缺乏信息道歉,并将最详细的答案视为最好的答案。
回答by Android is everything for me
use this line in your manifest file against your activity in which you want your result hope it will help you
在您的清单文件中使用这一行来针对您希望获得结果的活动希望它对您有所帮助
android:windowSoftInputMode="adjustPan|stateAlwaysHidden"
like
喜欢
<activity
android:name="com.android.youractivity"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />
回答by LOG_TAG
Use this in onCreateDialog in BottomSheetFragment / Dailogfragment
在 BottomSheetFragment / Dailogfragment 的 onCreateDialog 中使用它
KeyboardUtil(getActivity(), view);
or
或者
For fragment use
片段使用
new KeyboardUtil(this, findViewById(R.id.fragment_container));
by using this Util class
通过使用这个 Util 类
Credit:Mikepenz
信用:迈克彭兹
回答by Kishore Reddy
Add this line of code in DialogFragment Oncreateview below R.layout.layout_ui
在R.layout.layout_ui下面的DialogFragment Oncreateview中添加这行代码
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);