【问题标题】:How to make textview, which is under edit text, is pushed up with keyboard showing如何使编辑文本下的文本视图通过键盘显示向上推
【发布时间】:2019-01-27 07:21:26
【问题描述】:

我有以下布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/DarkGray"
    android:orientation="vertical">


        <EditText
            android:id="@+id/etCaption"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1"           
            android:gravity="start|top" />

        <TextView
            android:id="@+id/tvCounter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_gravity="right" />


</LinearLayout>

本质上是编辑文本占据屏幕,文本视图位于底部。

当我点击编辑文本时,键盘会出现并覆盖底部的文本视图。

如何确保 textview 被向上推,以便用户在输入编辑文本时可以看到它。

我最初将它作为约束布局,我将其更改为线性布局,但我仍然无法让它工作

谢谢

【问题讨论】:

  • 使用 android.support.design.widget.TextInputLayout 可以实现类似的行为

标签: android


【解决方案1】:

在键盘打开时调整布局

试试这个代码: 清单

<activity android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

你要在哪个activity中调整键盘添加这一行

android:windowSoftInputMode="adjustResize"

查看屏幕截图:

我希望它对你有用

【讨论】:

    【解决方案2】:

    layout_height="match_parent" 的节点不会被推向任何方向,因为布局没有空间移动;这应该是layout_height="wrap_content"(“显示布局边界”)。 当键盘出现和消失时,将android:windowSoftInputMode="adjustResize""adjustPan|adjustResize" 添加到Manifest.xml 中的activity 应该会调整布局大小。

    Activity 的主窗口总是会调整大小,以便为屏幕上的软键盘腾出空间。

    这种(或非常相似的)效果由材料TextInputLayout提供:

    api "com.google.android.material:material:1.0.0"
    

    例如(注意app:counterEnabledapp:counterMaxLength 属性):

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/caption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:hint="@string/hint_caption"
            android:inputType="textNoSuggestions"
            android:text="@={item.caption}"
    
            app:counterEnabled="true"
            app:counterMaxLength="50"/>
    
    </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

    • 谢谢马丁。但是,如果我在根目录使用 wrap_parent,那么编辑文本将缩小为一行!我希望编辑文本占用尽可能多的空间。你有什么建议,所以出现下面的文本视图?我尝试了调整大小,但没有用。我不能使用材质组件,因为我正在使用 appcompat 并且上面的代码用于编辑文本。我希望 textview 出现
    【解决方案3】:

    您需要将此声明发送至manifest.xml

    android:windowSoftInputMode="stateVisible|adjustResize"
    

    确保它在声明textview 的活动下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多