有没有办法禁止键盘移动LinearLayout?

Posted

技术标签:

【中文标题】有没有办法禁止键盘移动LinearLayout?【英文标题】:Is there any way to forbid keyboard to move LinearLayout? 【发布时间】:2019-06-03 18:05:20 【问题描述】:

我的活动中有一个片段和线性布局。在这个片段中,我有一个文本视图。如果片段中没有 ScrollView,则一切正常。但如果有 ScrollView LinearLayout 则移动到键盘顶部。 这就是我的应用在文本上没有 Scrollview 时的外观:https://yapx.ru/v/EPcoQ 这就是它的外观:https://yapx.ru/v/EPcqd 我不喜欢键盘处于活动状态时底部的导航栏没有隐藏。我该怎么做才能让应用在第一张照片上看起来像并且让 textView 可滚动?

我尝试使用“windowSoftInputMode”来禁止键盘移动此 LinearLayout,但没有帮助。 我也试过“focusableInTouchMode”和“isScrollContainer” 属性,但它们也没用。

这是活动文件:

<?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:id="@+id/main_chetnost"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:background="#86A0FC"
    android:windowSoftInputMode="adjustResize"
    >


    <fragment
        android:id="@+id/fragment_theory"
        android:name="com.example.user.mathplace_1.theory"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

    <fragment
        android:id="@+id/fragment_task"
        android:name="com.example.user.mathplace_1.Task"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="MathPlace"
        app:titleTextColor="#fff">

        <ImageButton
            android:id="@+id/homeButton"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="end"
            android:layout_marginStart="8dp"
            android:layout_marginTop="6dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="6dp"
            android:background="@drawable/home"
            android:contentDescription="@string/report_description"
            android:onClick="delete"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.887"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.987" />

    </android.support.v7.widget.Toolbar>



    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_
        android:layout_
        android:background="#FFF"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        android:focusableInTouchMode="false"
        android:isScrollContainer="false">

        <HorizontalScrollView
            android:layout_
            android:layout_
            android:background="#accbff"
            android:orientation="horizontal"
            android:focusableInTouchMode="false"
            android:isScrollContainer="false">

            <LinearLayout
                android:id="@+id/LinearLayout"
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:focusableInTouchMode="false"
                android:isScrollContainer="false"/>
        </HorizontalScrollView>

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

这是片段代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:background="@color/background"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/editText"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Введите ваш ответ"
        android:inputType="number"
        android:textColor="#5B6175"
        android:textSize="23sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:layout_constraintVertical_bias="0.3" />


    <ImageButton
        android:id="@+id/send"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/send"
        android:onClick="send"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:layout_constraintVertical_bias="0.3" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4">

        <ScrollView
            android:id="@+id/scrollView4"
            android:layout_
            android:layout_>

            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_
                    android:layout_
                    android:background="@drawable/text_rounded_corner"
                    android:padding="8dp"
                    android:text="dddddddddddddddddddddddd"
                    android:textColor="#5B6175"
                    android:textSize="20sp" />
            </LinearLayout>
        </ScrollView>
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/textView"
        android:layout_
        android:layout_
        android:background="@drawable/title_rounded_corner"
        android:padding="8dp"
        android:text="Заголовок"
        android:textColor="#FFF"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent" />
    <ImageView
        android:id="@+id/star2"
        android:layout_
        android:layout_
        android:layout_marginEnd="44dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/star" />

    <ImageView
        android:id="@+id/star1"
        android:layout_
        android:layout_
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/star" />

    <ImageView
        android:id="@+id/star3"
        android:layout_
        android:layout_
        android:layout_marginEnd="76dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/star" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

你能展示你的完整布局xml文件吗? 我已将完整的 xml 代码添加到问题中。 【参考方案1】:

我发现了如何在不破坏旧应用视图的情况下使我的文本可滚动。 我只需要在java文件中添加一行:

myTextview.setMovementMethod(new ScrollingMovementMethod());

【讨论】:

以上是关于有没有办法禁止键盘移动LinearLayout?的主要内容,如果未能解决你的问题,请参考以下文章

android LinearLayout中嵌套一个textview,当textview更新时会触发LinearLayout也更新。用啥办法可以让

有没有办法选择没有输入[类型]的移动键盘?

禁止移动端input弹出软键盘

如何允许移动构造并禁止分配和复制类的构造

Android点击EditText文本框之外任何地方隐藏键盘的解决办法

Android:显示键盘将我的组件向上移动,我想隐藏它们