<EditText
android:id="@+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<ListView
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"
>
**<any layout**
这是唯一对我有用的解决方案 .
0
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
15 回答
我面临的类似问题我的布局包括里面的滚动视图 . 每当我尝试在EditText中选择文本时,复制/剪切/粘贴操作栏都会向下移动,因为它不会调整布局大小
通过将其修改为如下
1)AndroidManifest.xml
2)style.xml文件,在活动样式中
有效 . !!!!!!!!!!!!
在AndroidManifest.xml中,不要忘记设置:
对于ScrollView中的RelativeLayout,设置:
会没事儿的
for line中的oncreate methode insert中的Activity
以及onCreate方法中的片段
更改您的清单文件的活动,如
OR
在活动类中对
onCreate()
方法进行更改,例如是的,请查看this article on the Android developers' site,它描述了框架如何处理出现的软键盘 .
android:windowSoftInputMode
属性可用于指定基于每个活动的内容:布局是否已调整大小或是否滚动等 .您也可以在
onCreate()
方法中使用此代码:为您的活动添加AndroidManifest.xml:
我尝试了迭戈拉米雷斯的方法,它有效 . 在AndroidManifest中:
在activity_main.xml中:
滚动屏幕有3个步骤 .
<RelativeLayout的
机器人:layout_width = “FILL_PARENT”
机器人:layout_height = “match_parent”
android:layout_gravity =“center”>
<TextView />
.........
<TextView />
<EditText>
<requestFocus />
</的EditText>
<按钮/>
</ RelativeLayout的>
</滚动型>
附:不要忘记在父相对布局中添加android:layout_gravity =“center” .
我找到了解决问题的方法,它与你的问题非常相似 .
首先,我只有一个包含2个元素的LinearLayout,一个ImageView和一个包含2个EditText和一个Button的LinearLayout,所以我需要在没有键盘的情况下将它们分开全屏,当键盘出现时它们应该看得更近 .
所以我添加了它们之间的视图,其高度属性为0dp,权重为1,这让我可以将视图彼此分开 . 当键盘出现时,由于它们没有固定高度,它们只是调整大小并保持方面 .
瞧!当键盘存在或不存在时,布局调整大小并且我的视图之间的距离相同 .
这是完整的清单代码
如果你是碎片,那么你必须在你的活动中将以下代码添加到你的onCreate中,它解决了我的问题
好吧,这已经很晚了但是我发现如果你在编辑文本下添加一个列表视图,那么键盘将移动该edittext下的所有布局而不移动ListView
这是唯一对我有用的解决方案 .
在清单文件中添加以下行 . 它会工作