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

如何为Material Design的TextInputLayout内的TextInputEditText设置顶部对齐

解决TextInputEditText文字顶部对齐的问题

要让TextInputEditText内的文字顶部对齐,直接给它添加android:gravity="top"属性即可——TextInputEditText继承自EditText,gravity属性负责控制文字的对齐方向,属性面板里可能没直接显示这个选项,手动写进布局代码就行。

修改后的完整布局代码如下:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etCreatePost"
        android:layout_width="0dp"
        android:layout_height="260dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:hint="What do you want to talk about?"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="@style/edit_text_gravity">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="top"/>

    </com.google.android.material.textfield.TextInputLayout>

如果需要复用这个样式,也可以把android:gravity="top"添加到专门的EditText样式中,再通过style属性应用到TextInputEditText上。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 12:40:37