Android 打开键盘时,背景图像向上滑动
我在android中构建登录页面时面临问题。我想保持用户名和登录在我的布局底部。但当输入用户名/密码时,键盘会弹出,我的背景图像也会随之滑动。我希望我的背景图像应该是静态的,当键盘弹出时,只有我的登录布局应该向上滑动。我在活动中添加了带有登录布局的片段,并在活动布局中添加了背景图像 以下是我的片段和活动布局代码: fragment_login.xmlAndroid 打开键盘时,背景图像向上滑动,android,android-layout,android-fragments,android-activity,Android,Android Layout,Android Fragments,Android Activity,我在android中构建登录页面时面临问题。我想保持用户名和登录在我的布局底部。但当输入用户名/密码时,键盘会弹出,我的背景图像也会随之滑动。我希望我的背景图像应该是静态的,当键盘弹出时,只有我的登录布局应该向上滑动。我在活动中添加了带有登录布局的片段,并在活动布局中添加了背景图像 以下是我的片段和活动布局代码: fragment_login.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr
<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:gravity="center_horizontal">
<RelativeLayout android:id="@+id/login_form" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:id="@+id/username_login_form" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical">
<com.test.views.CustomEditText android:id="@+id/usernameview" android:layout_width="@dimen/login_edittext_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="12dp" android:gravity="center_horizontal"
android:hint="@string/prompt_username" android:imeOptions="actionNext" android:inputType="text|textPersonName|textFilter|textNoSuggestions" android:maxLines="1" android:padding="16dp" android:paddingLeft="5dip" android:scrollHorizontally="true"
android:singleLine="true" android:textColor="@android:color/white" android:textColorHint="@android:color/white" android:textSize="16sp" />
<com.test.views.CustomEditText android:id="@+id/password" android:layout_width="@dimen/login_edittext_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="8dp" android:gravity="center_horizontal"
android:hint="@string/prompt_password" android:imeOptions="actionNext" android:inputType="textPassword" android:maxLines="1" android:padding="16dp" android:singleLine="true" android:textColor="@android:color/white" android:textColorHint="@android:color/white"
android:textSize="16sp" />
<Button android:id="@+id/sign_in_button" android:layout_width="@dimen/login_button_width" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="8dp" android:background="@drawable/blue_btn_selector" android:imeOptions="actionDone"
android:paddingLeft="26dp" android:paddingRight="26dp" android:text="@string/sign_in" android:textColor="@android:color/white" android:textSize="@dimen/signin_button_textsize" android:textStyle="bold" />
<TextView android:id="@+id/forgot_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/forgot_password" android:textColor="@android:color/white" android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/username_login_form" android:layout_centerHorizontal="true" android:layout_marginBottom="25dp" android:text="text" android:textColor="@android:color/white"
android:textSize="42sp" />
</RelativeLayout>
<!-- Login progress -->
<ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLargeInverse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginBottom="8dp" android:visibility="gone"
/>
</RelativeLayout>
您必须将背景设置为窗口而不是布局,这样背景就不依赖于布局更改。榜样
getActivity().getWindow().setBackgroundDrawableResource(yourBackgroundid);
在某些设备中,上述代码不会出现问题,因此您必须这样做
getActivity().getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
getActivity().getWindow().setBackgroundDrawableResource(yourBackgroundid);
在Java代码中使用以下代码:
getActivity().getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.imagename));
在AndroidManifest.xml中,在活动中添加以下代码:
android:windowSoftInputMode="adjustResize"
我没有看到背景图像向上滑动!只有EditText向上滑动,这是预期的行为!
android:windowSoftInputMode="adjustResize"