[英]Android prevent layout from moving up on focus on edit field
嗨,我搜索了很多东西,但是没有什么可以解决我的问题。 这是布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LoginFragment_Layout_Out"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background"
android:gravity="center"
android:orientation="vertical"
android:focusable="false"
android:focusableInTouchMode="false"
>
<RelativeLayout
android:id="@+id/login_input_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/LoginFragment_Password_Password"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_above="@+id/LoginFragment_Button_Login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:background="@color/grau"
android:ems="10"
android:gravity="center"
android:fontFamily="sans-serif"
android:hint="@string/LoginFragment_Password_Hint"
android:inputType="textPassword"
android:textColor="@color/black"
android:textColorHint="@color/black"
/>
<EditText
android:id="@+id/LoginFragment_EditText_UserName"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_above="@+id/LoginFragment_Password_Password"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:background="@color/grau"
android:ems="10"
android:gravity="center"
android:inputType="text"
android:hint="@string/LoginFragment_EditText_UserName_Hint"
android:textColor="@color/black"
android:textColorHint="@color/black" >
</EditText>
<Button
android:id="@+id/LoginFragment_Button_Login"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignEnd="@+id/LoginFragment_Password_Password"
android:layout_alignParentBottom="true"
android:background="@color/grau"
android:hint="@string/LoginFragment_Button_Login"
android:textColor="@color/black"
android:textColorHint="@color/black" />
<Button
android:id="@+id/LoginFragment_Button_Registration"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignStart="@+id/LoginFragment_Password_Password"
android:layout_alignBottom="@+id/LoginFragment_Button_Login"
android:background="@color/grau"
android:onClick="startRegister"
android:text="@string/LoginFragment_Button_Registration"
android:textColor="@color/black"
android:textColorHint="@color/black" />
</RelativeLayout>
而且我不希望因为背景图像而调整布局的大小。因此,我将使用此代码向上移动包含编辑字段的布局部分
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
View rootLay = rootView.findViewById(R.id.LoginFragment_Layout_Out);
RelativeLayout inputLay = (RelativeLayout) rootView.findViewById(R.id.login_input_layout);
rootLay.getWindowVisibleDisplayFrame(r);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int height = rootLay.getRootView().getHeight();
int heightDiff = rootLay.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
lp.setMargins(0, 0, 0, heightDiff);
inputLay.setLayoutParams(lp);
}else{
lp.setMargins(0, 0, 0, 0);
inputLay.setLayoutParams(lp);
}
}
});
这也可以找到,但是当我第一次单击编辑字段时,出现键盘时,rootlayout也向上移动,但是当我关闭键盘并再次单击编辑字段时,一切都正常。.为什么请帮助
您为什么不只设置adjustResize,以便窗口不移动?
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="@string/app_name" >
</activity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.