【问题标题】:How to prevent edit text from overlapping the view below?如何防止编辑文本与下面的视图重叠?
【发布时间】:2020-09-17 07:03:52
【问题描述】:

我正在制作一个日记应用程序。所以我做了一个Linearlayout(我会称之为布局工具箱)的工具,需要像这个图像一样编写。
image
但是我输入文本并且编辑文本变得更长,工具箱与编辑文本的底部重叠。所以看不到我正在输入的文字。
我进行了很多谷歌搜索,并使用了一种方法来为布局添加底部边距,但它没有用。
issue GIF here(因为隐藏在工具箱里,可以看到我之前输入的文字,而不是我输入的文字。)

在我添加滚动视图之前存在这个问题。我猜这个问题与滚动视图无关。

如何防止编辑文本与下面的视图重叠?

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".DiaryActivity">

<RelativeLayout
    android:id="@+id/save_tap"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@+id/top_line"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    //elements
</RelativeLayout>

<View
    android:id="@+id/top_line"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:background="#D1D1D1"
    app:layout_constraintTop_toBottomOf="@+id/save_tap" />

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintTop_toBottomOf="@+id/top_line">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@id/toolbox">

        <EditText
            android:id="@+id/edit_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|start"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:hint="@string/hint_diary_title"
            android:textColor="@android:color/black"
            android:textSize="23sp" />

        <EditText
            android:id="@+id/edit_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:background="@null"
            android:ems="10"
            android:gravity="start|top"
            android:hint="@string/hint_diary_contents"
            android:inputType="textMultiLine"
            android:textColor="#515151" />
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/toolbox"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/top_dividing_line"
    android:gravity="center|left"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    //image buttons
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-edittext android-scrollview


    【解决方案1】:

    尝试添加一个同时采用滚动视图和工具箱布局的线性布局。

    你还可以显示你所有的 xml(包括父布局吗?)

    【讨论】:

    • 你的意思是做一个新的线性布局,包括滚动视图和工具箱?我试过了,但结果没有改变。而且我是用XML编辑的,包括父布局,所以请再看一遍。
    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical">
    
    <RelativeLayout
        android:id="@+id/save_tap"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@android:color/black"
        android:orientation="horizontal">
    
    </RelativeLayout>
    
    <View
        android:id="@+id/top_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:background="#D1D1D1"
        app:layout_constraintTop_toBottomOf="@+id/save_tap" />
    
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:fillViewport="true"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/top_line">
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@id/toolbox">
    
            <EditText
                android:id="@+id/edit_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top|start"
                android:layout_marginTop="20dp"
                android:background="@null"
                android:textColor="@android:color/black"
                android:textSize="23sp" />
    
            <EditText
                android:id="@+id/edit_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine"
                android:textColor="#515151" />
        </LinearLayout>
    </ScrollView>
    
    <LinearLayout
        android:id="@+id/toolbox"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center|left"
        android:orientation="horizontal">
    </LinearLayout>
    </LinearLayout>
    

    我添加了一些颜色来显示不同的布局 告诉我这是不是你想要的行为

    【讨论】:

    • 我试过了,结果没有变化。? 之前加滚动视图就出现了这个问题。我猜这个问题与滚动视图无关。
    • 能否请您添加屏幕截图,我想我误解了您的问题,因为它与我的布局配合得很好
    • 我更新了问题 GIF。请再检查一次。非常感谢你一直回复我。???
    • 很抱歉,但我不知道,我的布局工作正常,你在使用片段吗?它也可能来自您的清单
    猜你喜欢
    • 2020-05-09
    • 2021-11-17
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多