【问题标题】:android - ScrollView doesn't scroll when keyboard is upandroid - 键盘启动时 ScrollView 不滚动
【发布时间】:2016-05-30 20:41:18
【问题描述】:

我在 Android Studio 中有以下 xml 文件:

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="mk.klikniobrok.klikniobrok.RegisterActivity">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="48dp">


        <RelativeLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/layout_top_margin"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <LinearLayout
                android:id="@+id/input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:orientation="vertical">


                <EditText
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/name_hint"
                    android:inputType="text"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

                <EditText
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/user_hint"
                    android:inputType="text"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/pass_hint"
                    android:inputType="textPassword"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

                <EditText
                    android:id="@+id/repeatPwd"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/repeatPwd_hint"
                    android:inputType="textPassword"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

                <EditText
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/email_hint"
                    android:inputType="textEmailAddress"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

                <EditText
                    android:id="@+id/phone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/phone_hint"
                    android:inputType="phone"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:textSize="@dimen/input_text_size" />

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:gravity="end"
        android:orientation="horizontal"
        android:paddingBottom="2dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="2dp">

        <Button
            android:id="@+id/registerButton"
            style="@style/RobotoBoldButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/ripple_transparent"
            android:text="РЕГИСТРИРАЈ СЕ"
            android:textSize="16sp" />
    </LinearLayout>
</RelativeLayout>

当键盘未启动时,我可以看到每个元素,但每当键盘启动时,ScrollView 不会向下滚动以显示被覆盖的元素。我已通读 StackOverflow 并将 android:windowSoftInputMode="stateHidden|adjustResize" 添加到 AndroidManifest.xml 但它仍然无法正常工作。

发生了什么问题,我该如何解决?

【问题讨论】:

  • 我刚刚在一个测试项目中尝试了你的布局。它为我滚动,即使没有adjustResize。您可以在其他设备或模拟器上尝试吗?您是否有任何机会在调试器中暂停?
  • @MarcinKoziński 我在 3 台设备上试用过,两台 pre-Lollipop 和一台运行 vanilla Android M,它只适用于 1。这很奇怪。
  • 尝试去掉ScrollView里面的RelativeLayout。这不是必需的,尽管它也应该与它一起使用。

标签: android android-layout android-xml android-scrollview


【解决方案1】:

请尝试在根元素上设置android:layout_height="match_parent"

你的根元素有android:layout_height="wrap_content"(根据你的孩子计算你的身高),它的主要孩子有android:layout_height="match_parent"(根据你的父母计算你的身高)。它看起来像先有鸡还是先有蛋的问题,我想这就是为什么它在某些设备上有效而在其他设备上无效的原因。

说实话,我还没有尝试过这是否有效,但这就是我的结论。如果没有帮助,我再看看。

【讨论】:

    【解决方案2】:

    已修复,问题是在 LinearLayout 中使用上边距而不是元素的填充,显然这是一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多