5

我有一个相当简单的登录页面在我的Android应用程序,包含标题,用户名TextView的,密码的TextView,登录按钮和图像页脚:如何避免键盘启动或方向改变时Android界面的变化?

<?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. 屏幕水平取向时也是如此。

我试过的东西:

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

都没有工作。 我将不胜感激任何帮助。

回答

1

我结束了使用RelativeLayout的能力解决这样的: 我添加了一个ID为“登陆”命名为“形式”的LinearLayout中,并加入这一行到被隐藏的按钮后面的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> 

感谢。

1

在您的活动选项卡中打开您的manifest.xml文件.change。

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

您是否也指我应该在onConfigurationChanged方法中写入内容?因为我不知道在配置更改时要更改什么......只有编写您的建议不起作用......谢谢 – MichalK 2012-08-02 10:44:55

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

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