2012-05-15 116 views
13

我有一些领域的视图(姓名,电子邮件,密码,注册按钮):如何在键盘出现时使android视图滚动?

<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场,键盘出现并覆盖在视图下方区域。所以当键盘出现时我看不到它们。我想知道如何使视图滚动,以便我可以看到较低的字段。此外,如何自动滚动视图以使焦点字段可见。

回答

37

您需要包括android:windowSoftInputMode="adjustResize"属性在AndroidManifest.xml文件的活动 - 然后Android的应该为你做自动调整大小:

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

实际上它被设置为“adjustPan | adjustResize”。取下adjustPan使其工作。谢谢! – Alexis

0

你有没有尝试过这样的:

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

6

我已经创建了简单的xml文件,希望这是你在找什么。

步骤1。您可以在键盘出现时滚动。

第2步。当你点击文本字段时,它会出现在Focus /上方的键盘上。

<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> 
+0

我遇到了滚动视图的问题,fillViewport正是我所需要的。 – AlexBrand

0

在我的情况下,@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> 
相关问题