0

嗨,我搜索了很多,但没有什么能解决我的问题。这是布局

<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);
             }
        }
    });

这也可以找到,但是当我第一次单击编辑字段时,当keybpard出现时rootlayout也向上移动但是当我关闭键盘并第二次单击编辑字段时一切正常..为什么请帮忙

4

1 回答 1

0

为什么不直接设置 adjustResize 以使窗口不移动?

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize"
    android:label="@string/app_name" >
</activity>
于 2014-09-18T14:50:09.723 回答