【问题标题】:android scrollview not scrolling down after keyboard opens键盘打开后android滚动视图不向下滚动
【发布时间】:2013-11-23 19:50:17
【问题描述】:

我有一个位于滚动视图内的文本视图,它可以正常滚动,直到打开软键盘。

当键盘打开时,它就像是随着键盘的高度再次向上滚动。

我的尝试

我在清单中尝试了这个,但产生了完全相同的结果

android:windowSoftInputMode="adjustResize"

然后这个:

android:windowSoftInputMode="adjustPan"

这似乎可行,但问题是它正在向上移动整个屏幕并将标题移出视线。

我也尝试在线性布局中添加以下内容

android:focusable="true"
android:focusableInTouchMode="true"

这只导致应用程序不关注输入字段(EditText)并且键盘没有自动打开,但是当您单击输入字段时,它的行为与以前相同。

这是 XML 文件代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom_layout"
    android:layout_marginTop="10dip" >

    <LinearLayout
        android:id="@+id/msg_list_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>

<RelativeLayout
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/background_light">
    <Button
        android:id="@+id/send_btn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/txt_send" />

    <EditText
        android:id="@+id/msg_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/send_btn"
        android:layout_toLeftOf="@+id/send_btn"
        android:inputType="text" >
    </EditText>
</RelativeLayout>

有什么建议吗?

【问题讨论】:

标签: android android-layout


【解决方案1】:

我遇到了同样的问题,解决方案是这样的: 我的活动处于fullscreen 模式,滚动在此模式下不起作用,这是一个错误,我们向谷歌发送报告。 只需查看您在manifest 中的活动,如果有全屏模式,请将其删除。

【讨论】:

  • 在这个问题上花 1 小时,谢谢!你是怎么想出来的?
【解决方案2】:

我遇到了这个问题,很头疼。
解决方法不是从屏幕顶部绘制 XML 布局,而是从屏幕底部绘制。

看看这些例子:

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent">

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include layout="@layout/main_contents"/>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

ma​​in_contents.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
    android:paddingBottom="20dp">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.92" />


    <android.support.v7.widget.CardView
        android:id="@+id/card1"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_margin="30dp"
        android:padding="10dp"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/sam3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="sample"
                android:textColor="#673AB7"
                android:textSize="23sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />



                <EditText
                    android:id="@+id/sam2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical"
                    android:backgroundTint="#673AB7"
                    android:digits="0123456789"
                    android:gravity="center"
                    android:hint="sample"
                    android:inputType="phone"
                    android:maxLength="11"
                    android:maxLines="1"
                    android:nextFocusDown="@id/sam1"
                    android:textColor="#673AB7"
                    android:layout_marginStart="40dp"
                    android:layout_marginEnd="40dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/sam3"
                    app:layout_constraintVertical_bias="0.3"/>


        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>


    <Button
        android:id="@+id/sam1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:background="#673AB7"
        android:text="sample"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/card1"
        app:layout_constraintEnd_toEndOf="@+id/card1"
        app:layout_constraintStart_toStartOf="@+id/card1"
        app:layout_constraintTop_toBottomOf="@+id/card1" />


</android.support.constraint.ConstraintLayout>

ma​​in_contents2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />


    <android.support.v7.widget.CardView
        android:id="@+id/card1"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_margin="30dp"
        android:padding="10dp"
        app:cardCornerRadius="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/sam2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_marginStart="40dp"
                android:layout_marginEnd="40dp"
                android:backgroundTint="#673AB7"
                android:digits="0123456789"
                android:gravity="center"
                android:hint="sample"
                android:inputType="phone"
                android:maxLength="11"
                android:maxLines="1"
                android:nextFocusDown="@id/sam1"
                android:textColor="#673AB7"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/sam3"
                app:layout_constraintVertical_bias="0.3" />


            <TextView
                android:id="@+id/sam3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="sample"
                android:textColor="#673AB7"
                android:textSize="23sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>


    <Button
        android:id="@+id/sam1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:background="#673AB7"
        android:text="sample"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/card1"
        app:layout_constraintEnd_toEndOf="@+id/card1"
        app:layout_constraintStart_toStartOf="@+id/card1"
        app:layout_constraintTop_toBottomOf="@+id/card1" />


</android.support.constraint.ConstraintLayout>

第二个 XML 文件 main_contents2 从顶部绘制,而 android 只是将 EditText 放在键盘顶部。但是 main_contents 从屏幕底部。所以当软键盘出现时,布局会调整大小。

【讨论】:

    【解决方案3】:

    尝试将滚动视图的 layout_height 更改为 wrap_content。

    【讨论】:

    • 这给了我同样的结果
    • 在清单中添加这个。 android:windowSoftInputMode="adjustPan|adjustResize" 我试过了,它对我有用。
    • @user2925016 谢谢,但是软键盘现在在我的输入框上方,所以用户看不到他们在输入什么?
    【解决方案4】:

    你需要做的就是

    android:isScrollContainer="true"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2016-12-17
      • 2022-10-09
      • 1970-01-01
      相关资源
      最近更新 更多