如果可以看到软键盘,则ScrollView不会滚动

时间:2012-06-29 14:45:04

标签: android android-layout scrollview

我在登录屏幕上使用了一个简单的布局,其中包含顶部的栏和用户登录或转到注册活动所需的项目。

所有元素都非常适合屏幕,直到出现虚拟键盘。 Android软键盘模糊了表单的一部分,我想使用ScrollView,以便用户可以滚动并能够看到所有元素。

但是,即使使用ScrollView,我也无法滚动查看页面底部:

<?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:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFCB05"
android:orientation="vertical"
android:isScrollContainer="true"
android:gravity="top" >

<include
    android:id="@+id/include1"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    layout="@layout/actionbar_layout" />

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_below="@+id/include1"
 >



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/usuario"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/txtUsuario"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:hint="@string/usuario" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="15dip" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/senha"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/txtSenha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_centerVertical="true"
        android:layout_marginTop="5dip"
        android:ems="10"
        android:hint="@string/senha"
        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="right" >

    <Button
        android:id="@+id/logarBtn"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dip"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="15dip"
        android:text="@string/logar" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/registerBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logarBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="60dip"
        android:linksClickable="true"
        android:text="@string/cadastrar"
        android:textColor="#0000CC"
        android:textSize="18dp" />
</LinearLayout>

</LinearLayout>

</RelativeLayout>
</ScrollView>

我想我可以这样做:Android Soft Keyboard Obscures EditTexts in ScrollView但这似乎不是最好的做法。

有没有人知道让它正确滚动的更好方法?

3 个答案:

答案 0 :(得分:2)

查看以下链接:

Android: ScrollView not scrolling with keyboard out

这个解决方案对我有用。

答案 1 :(得分:2)

public class CustomScrollView extends ScrollView {

  private int scrollOffset = 0;

  public CustomScrollView (final Context context, final AttributeSet attrs) {
    super(context, attrs);
  }

  public void setScrollOffset (final int scrollOffset) {
    this.scrollOffset = scrollOffset;
  }

  @Override
  protected int computeScrollDeltaToGetChildRectOnScreen (final Rect rect) {
    // adjust by scroll offset
    int scrollDelta = super.computeScrollDeltaToGetChildRectOnScreen(rect);
    int newScrollDelta = (int) Math.signum(scrollDelta) * (scrollDelta + this.scrollOffset);
    return newScrollDelta;
  }
}

使用此自定义滚动视图而不是普通的滚动视图。更改scrollOffset的值以获得所需的结果(scrollOffset = 10表示当软键盘打开时将向下滚动额外的10dp)确保在活动中添加了android:windowSoftInputMode="adjustResize"

答案 2 :(得分:0)

理想的方法如下。

android:windowSoftInputMode="adjustResize"

但是,当视图已经适合屏幕内部时,即使打开软键盘,也会禁止滚动。

相关问题