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

时间:2012-08-01 07:20:16

标签: android android-layout screen android-softkeyboard screen-orientation

我的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>

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

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

    1. 在活动android:windowSoftInputMode="adjustPan"中进行设置,但我希望能够在键盘出现时向下滚动,或android:windowSoftInputMode="adjustResize"或其他任何参数。
    2. 在按钮中设置marginBottom
    3. 都没有奏效。 我将不胜感激任何帮助。

3 个答案:

答案 0 :(得分:1)

在您的活动标签中打开您的manifest.xml文件.change。

<activity
            android:configChanges="keyboardHidden|orientation" 
            android:name=".testActivity"
            android:label="@string/app_name"></activity>

答案 1 :(得分:1)

我最终通过使用RelativeLayout功能来解决这个问题: 我在名为“form”的“Login Form”的LinearLayout中添加了一个id,并将此行添加到隐藏在按钮后面的ImageView:android:layout_below =“@ + id / form”。 然后做了一些化妆品的改变。 完整代码如下:

<?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>

感谢。

答案 2 :(得分:0)

<activity
            android:configChanges="adjustPan" 
            android:name=".testActivity"
            android:label="@string/app_name"></activity>

请参阅“android:windowSoftInputMode”。可用模式here