【问题标题】:how to make an android view scrollable when the keyboard appears?键盘出现时如何使android视图可滚动?
【发布时间】:2012-05-15 11:06:49
【问题描述】:

我有一些字段(姓名、电子邮件、密码、注册按钮)的视图:

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

    <ViewFlipper
        android:id="@+id/viewflipper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        //Layouts

    </ViewFlipper>

</ScrollView>

当我关注一个edittext 字段时,键盘会出现并覆盖视图上的下部字段。所以当键盘出现时我看不到它们。我想知道如何使视图可滚动,以便我可以看到较低的字段。此外,如何使视图自动滚动以使焦点字段可见。

【问题讨论】:

    标签: android android-layout android-scrollview android-keypad


    【解决方案1】:

    您需要在 AndroidManifest.xml 文件中为您的活动添加 android:windowSoftInputMode="adjustResize" 属性 - 然后 Android 会自动为您调整大小:

    <activity android:name="..."
              ...
              android:windowSoftInputMode="adjustResize">
        ...
    />
    

    【讨论】:

    • 实际上它被设置为“adjustPan|adjustResize”。删除 adjustPan 使它工作。谢谢!
    【解决方案2】:

    我已经创建了简单的 xml 文件。

    第 1 步。出现小键盘时可以滚动。

    第 2 步。当您单击文本字段时,它将出现在焦点/键盘上方。

    <ScrollView android:id="@+id/ScrollView01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillViewport="true" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:weightSum="1.0" 
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:id="@+id/l_layout">
        <EditText 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:id="@+id/editText1" 
            android:layout_marginTop="100dp">
            <requestFocus></requestFocus>
        </EditText>
        <EditText 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:id="@+id/editText2" 
            android:layout_marginTop="100dp">
            </EditText>
        </LinearLayout> 
    </ScrollView>
    

    【讨论】:

    • 我的滚动视图有问题,而 fillViewport 正是我所需要的。
    【解决方案3】:

    在我的情况下,@Aleks G 解决方案不起作用。所以我用

    解决了我的问题
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/registration_scroll_view"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/btn_register_submit"
        >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">
    
           <!--This comment will be replaced 
               by your focusable views-->
    
        </LinearLayout>
    </ScrollView>
    
    <Button
        android:id="@+id/btn_register_submit"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/form_action_button_green"
        android:gravity="center"
        android:text="@string/submit"
        />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:
      <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
      
      <ViewFlipper
          android:id="@+id/viewflipper"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" >
          <ScrollView 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@+id/scroll_container">
              //Layouts
          </ScrollView>
          
      
      </ViewFlipper>
      

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 2012-02-01
        • 2014-11-10
        • 1970-01-01
        • 2012-06-29
        • 1970-01-01
        • 2014-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多