You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中TextInputLayout高度异常问题求助

解决TextInputLayout输入内容时显示过高的问题

问题原因分析

  • 你给TextInputEditText设置了固定高度android:layout_height="50dp",但Material组件的TextInputLayout自带顶部提示栏、边框等装饰元素,固定子控件高度会迫使父布局为容纳这些装饰拉伸整体高度。
  • 自定义字体@font/poppins_r的行高或字号可能与组件默认样式不兼容,导致内部排版异常撑开布局。

解决方案

方案1:移除TextInputEditText的固定高度

将TextInputEditText的固定高度改为wrap_content,让子控件高度由内容和组件默认样式决定,TextInputLayout会自动适配合理高度。修改后代码如下:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_cost_of_service"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ic_logo_binar"
    app:boxStrokeColor="@color/black"
    android:textColorHint="@color/black"
    app:startIconDrawable="@drawable/baseline_monetization_on_24"
    app:startIconTint="@color/black">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tie_cost_of_service"
        android:layout_width="252dp"
        android:layout_height="wrap_content"
        android:drawablePadding="8dp"
        android:fontFamily="@font/poppins_r"
        android:hint="@string/cost_of_service"
        android:inputType="numberDecimal"
        android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>

方案2:调整内边距与字体参数(需保留近似高度时用)

如果需要保持接近50dp的高度,可以通过以下方式调整:

  • 给TextInputLayout添加垂直内边距,减少额外空间
  • 给TextInputEditText设置Material组件默认的最小高度基准,同时关闭字体自带的上下内边距

示例代码:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_cost_of_service"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:paddingVertical="2dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ic_logo_binar"
    app:boxStrokeColor="@color/black"
    android:textColorHint="@color/black"
    app:startIconDrawable="@drawable/baseline_monetization_on_24"
    app:startIconTint="@color/black">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tie_cost_of_service"
        android:layout_width="252dp"
        android:layout_height="wrap_content"
        android:minHeight="46dp"
        android:includeFontPadding="false"
        android:drawablePadding="8dp"
        android:fontFamily="@font/poppins_r"
        android:hint="@string/cost_of_service"
        android:inputType="numberDecimal"
        android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>

方案3:更新Material组件库版本

若你使用的Material Components库版本较旧,可能存在布局计算bug,可在app模块的build.gradle中更新至最新稳定版本:

implementation 'com.google.android.material:material:1.11.0' // 替换为当前最新稳定版本

内容的提问来源于stack exchange,提问作者Razer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 04:55:19