1

我正在设计一个简单的登录活动。我使用了以下 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal"
        android:layout_margin="15dp">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My application title"
            android:textSize="30sp"/>
    </LinearLayout>

     <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical"
        android:layout_margin="15dp">
          <EditText 
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Username"/>      
          <EditText 
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Password"
              android:layout_marginTop="15dp"/>
          <Button 
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="15dp"
              android:text="Login"/>
     </LinearLayout>

</LinearLayout>

我得到了这个结果:

在此处输入图像描述

但如果重点是第一个 EditText 我得到这个:

在此处输入图像描述

如果焦点在第二个 EdiText 上,则结果如下:

在此处输入图像描述

考虑到我在 Manifest 中为我的活动使用了以下行:

android:windowSoftInputMode="adjustPan|adjustResize"

我希望当虚拟键盘出现时,两个编辑文本、按钮和文本视图在可用屏幕中可见并正确居中。我必须在我的代码中更改什么?

注意:如果我删除清单中的行,我会得到相同的结果:

android:windowSoftInputMode="adjustPan|adjustResize"
4

1 回答 1

1

You can replace the parent LinearLayout to a ScrollView :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <LinearLayout ...>

  ....
  ....

  </LinearLayout>


</ScrollView>

try this, it works for some of my apk !

Edit : sorry didn't see about center the text automaticaly, so with this you can adjust manually your editext to center with swipe your finger.. hope that will help

于 2014-02-28T11:30:10.623 回答