android - 显示软键盘时向上移动布局?
我在RelativeView中有一些元素,对齐底部属性设置,当软键盘出现时,元素被软键盘隐藏。
我希望它们向上移动,以便如果有足够的屏幕空间它们显示在键盘上方,或者使键盘上方的部分可滚动,那么用户仍然可以看到元素。
关于如何处理这个的任何想法?
15个解决方案
89 votes
是的,请查看关于Android开发者的这篇文章' 该网站描述了框架如何处理出现的软键盘。
Christopher Orr answered 2019-07-04T01:02:07Z
69 votes
您也可以在onCreate()方法中使用此代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Bobs answered 2019-07-04T01:02:32Z
31 votes
为您的活动添加AndroidManifest.xml:
android:windowSoftInputMode="adjustPan|adjustResize"
Serhii Bohutskyi answered 2019-07-04T01:02:56Z
22 votes
更改您的清单文件的活动,如
windowSoftInputMode="adjustResize"
要么
在活动类中更改onCreate()方法
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Jose Kurian answered 2019-07-04T01:03:30Z
15 votes
我面临的类似问题我的布局包括里面的滚动视图。每当我尝试在EditText中选择文本时,复制/剪切/粘贴操作栏都会向下移动,因为它不会调整布局大小
android:windowSoftInputMode="adjustPan"
通过将其修改为如下
1)AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
2)style.xml文件,在活动样式中
true
有效。!!!!!!!!!!!!
Ronak Poriya answered 2019-07-04T01:04:22Z
10 votes
在AndroidManifest.xml中,不要忘记设置:
android:windowSoftInputMode="adjustResize"
对于ScrollView中的RelativeLayout,设置:
android:layout_gravity="center" or android:layout_gravity="bottom"
会没事儿的
Zahra.HY answered 2019-07-04T01:05:00Z
7 votes
对于活动的oncreate方法插入下面的行
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
以及onCreate方法中的片段
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Rahul_Pawar answered 2019-07-04T01:05:32Z
5 votes
我尝试了迭戈拉米雷斯的方法,它有效。在AndroidManifest中:
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
...
在activity_main.xml中:
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
android:id="@+id/edName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/first_name"
android:inputType="textPersonName" />
...
CoolMind answered 2019-07-04T01:06:03Z
5 votes
滚动屏幕有3个步骤。
写入您的活动的清单文件:
机器人:windowSoftInputMode=" adjustResize"
在活动的oncreate()方法中添加以下行
getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)。
然后将整个视图放在" Scrollview" 在XML文件中:
android:layout_width="fill_parent"
android:background="@drawable/bg"
android:layout_height="match_parent"
>
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
.........
附: 不要忘记添加android:layout_gravity =" center" 在父相对布局中。
Heena Arora answered 2019-07-04T01:07:18Z
4 votes
我找到了解决问题的方法,它与你的问题非常相似。
首先,我只有一个包含2个元素的LinearLayout,一个ImageView和一个包含2个EditText和一个Button的LinearLayout,所以我需要在没有键盘的情况下将它们分开全屏,当键盘出现时它们应该看得更近。
所以我添加了它们之间的视图,其高度属性为0dp,权重为1,这让我可以将视图彼此分开。 当键盘出现时,由于他们没有固定高度,他们只需调整大小并保持方面。
瞧! 当键盘存在或不存在时,布局调整大小并且我的视图之间的距离相同。
Diego Ramírez Vásquez answered 2019-07-04T01:08:05Z
1 votes
这是完整的清单代码
android:name=".activities.MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="@string/app_name">
Dantalian answered 2019-07-04T01:08:30Z
1 votes
如果你是碎片,那么你必须在你的活动中将以下代码添加到你的onCreate中,它解决了我的问题
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Pratik Mohanrao Gondil answered 2019-07-04T01:08:56Z
0 votes
好吧,这已经很晚了但是我发现如果你在编辑文本下添加一个列表视图,那么键盘将移动该edittext下的所有布局而不移动ListView
android:id="@+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"//can make as 1dp
android:layout_below="@+id/et" // set to below editext
android:id="@+id/view"
>
**
这是唯一对我有用的解决方案。
Omar Boshra answered 2019-07-04T01:09:28Z
0 votes
Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.
public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();
public CommentKeyBoardFix(Activity activity)
{
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious)
{
int heightDifference = 0;
if (heightDifference > (usableHeightNow /4))
{
// keyboard probably just became visible
frameLayoutParams.height = usableHeightNow - heightDifference;
}
else
{
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightNow;
}
mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
return contentAreaOfWindowBounds.height();
}
}
And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java
yash786 answered 2019-07-04T01:09:46Z
0 votes
在清单文件中添加以下行。 它会工作
android:windowSoftInputMode="stateVisible|adjustResize">
...
Shivam Garg answered 2019-07-04T01:10:10Z