当键盘启动或方向改变时,如何避免Android UI的变化?

Mic*_*alK 5 android screen screen-orientation android-layout android-softkeyboard

我的Android应用程序中有一个相当简单的登录页面,包含标题,用户名TextView,密码TextView,登录按钮和图像页脚:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff" >

    <!-- Header  Starts -->

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >

        <!-- Logo Start -->

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:src="@drawable/header" />
        <!-- Logo Ends -->
    </LinearLayout>

    <!-- Header Ends -->

    <!-- Footer Start -->
    <LinearLayout android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="90dip"
    android:layout_alignParentBottom="true"
    android:gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:src="@drawable/footer" />
    </LinearLayout>
    <!-- Footer Ends -->

    <!-- Login Form -->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_below="@+id/header"
         >

        <!-- Application Name Label -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:gravity="center"
            android:padding="10dip"
            android:textSize="25dp"
            android:textStyle="bold"
        />

        <!-- Username Label -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="User Name"
            android:textColor="#372c24" />

        <EditText
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:singleLine="true" />
        <!-- Password Label -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Password"
            android:textColor="#372c24" />

        <EditText
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:password="true"
            android:singleLine="true" />
        <!-- Login button -->

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="Logon" />
        </LinearLayout>
        <!-- Login Form Ends -->

    </RelativeLayout>
 </ScrollView>
Run Code Online (Sandbox Code Playgroud)

我认为有两个问题是相关的:

  1. 键盘启动时,我向下滚动,我看到图像页脚"隐藏"在登录按钮后面,而不是停留在它下面.
  2. 当屏幕水平定向时也会发生同样的情况.

我试过的东西:

  1. 在活动中设置android:windowSoftInputMode="adjustPan",但我希望能够在键盘出现时向下滚动,或者android:windowSoftInputMode="adjustResize",或者任何其他参数.
  2. marginBottom在按钮中设置.

都没有奏效.我将不胜感激任何帮助.

Mic*_*alK 1

我最终通过使用RelativeLayout功能解决了这个问题:我在名为“form”的“登录表单”的LinearLayout中添加了一个id,并将这一行添加到隐藏在按钮后面的ImageView中: android:layout_below="@+身份证/表格”。然后做了一些外观上的改变。完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff" >

    <!-- Header  Starts -->
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <!-- Logo Start -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:src="@drawable/header" />
        <!-- Logo Ends -->

    </LinearLayout>
    <!-- Header Ends -->

    <!-- Login Form -->
    <LinearLayout
        android:id="@+id/form"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_below="@+id/header">

        <!-- Application Name Label -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:gravity="center"
            android:padding="10dip"
            android:textSize="25dp"
            android:textStyle="bold"/>

        <!-- Username Label -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="User Name"
            android:textColor="#372c24" />

        <EditText
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:singleLine="true" />

        <!-- Password Label -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Password"
            android:textColor="#372c24" />

        <EditText
            android:id="@+id/password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:password="true"
            android:singleLine="true" />

        <!-- Login button -->
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="Logon" />
    </LinearLayout>
    <!-- Login Form Ends -->

    <!-- Footer Start -->
    <LinearLayout android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="90dip"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|center"
        android:paddingBottom="10dip"
        android:layout_below="@+id/form"

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:src="@drawable/footer" />

        </LinearLayout>
        <!-- Footer Ends -->

    </RelativeLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

谢谢。