【问题标题】:Vertically centering TextInputLayout text with no hint enabled未启用提示的垂直居中 TextInputLayout 文本
【发布时间】:2020-07-09 13:21:24
【问题描述】:

所以我试图确保 TextInputEditText 中的主要文本(包裹在 TextInputLayout 中)垂直居中并使用默认的 Widget.MaterialComponents.TextInputLayout.FilledBox 样式,但是当提示被禁用时。

正如你在图片中看到的那样,app:hintEnabled="false",当前文本不是垂直居中的。

作为参考,我使用的是 1.1.0 版本的材料组件库。

我的布局文件:

<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">

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/set_pos_password_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:importantForAutofill="no"
            android:maxLength="4"
            android:text="1111"
            android:textAlignment="center" />
    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

结果:

【问题讨论】:

    标签: android android-textinputlayout material-components-android


    【解决方案1】:

    使用 layout_gravity = "Center"。

    【讨论】:

    • 感谢您的回复,不幸的是,这似乎没有使文本垂直居中,我已尝试将属性应用于外部 TextInputLayoutTextInputEditText
    • 请考虑提供代码 sn-p 或示例以及您相当简短的解释。
    【解决方案2】:

    调整 TextInputEditText 样式的底部填充。

    1. 在您的 TextInputLayout 样式中设置自定义主题覆盖
    2. 在继承自 editTextStyle 主题属性中使用自定义样式
    3. @style/Widget.MaterialComponents.TextInputEditText.FilledBox 默认情况下,FilledBox 样式应用以下默认填充:
     <item name="android:paddingTop">28dp</item>
     <item name="android:paddingBottom">12dp</item>
    

    您可以从 Material 文档中了解有关文本字段主题的更多信息。

    【讨论】:

    • 感谢您的回复,我希望有一个解决方案能够真正居中而不是仅仅调整填充直到看起来正确,除非我遗漏了什么,否则这个解决方案似乎有点 hacky?
    • 您是否尝试将重力属性添加到自定义editTextStyle?也看看这个answer 到一个类似的问题。
    • 不幸的是,添加重力没有效果。此外,您引用的答案似乎提供了一种使用标签水平居中的解决方案,而我希望它垂直居中且没有标签。
    【解决方案3】:

    这是一个实现这一目标的工作示例。我认为为什么这些建议还没有起作用的问题是您确实将填充应用于TextInputLayout,而不是嵌套的TextInputEditText。下面的定义将采取这种方式。

    XML

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.MyStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" >
    
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="2"
            android:text="10" />
    
    </com.google.android.material.textfield.TextInputLayout>
    

    样式定义

    <!-- Define a style 
    <style name="Widget.MaterialComponents.TextInputLayout.MyStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
        <item name="materialThemeOverlay">@style/ThemeOverlay.MaterialComponents.TextInputEditText.MyStyle</item>
        <item name="expandedHintEnabled">false</item>
        <item name="hintEnabled">false</item>
        <!-- below is optional -->
        <item name="boxCornerRadiusBottomEnd">4dp</item>
        <item name="boxCornerRadiusBottomStart">4dp</item>
        <item name="boxCornerRadiusTopEnd">4dp</item>
        <item name="boxCornerRadiusTopStart">4dp</item>
        <item name="boxStrokeWidth">0dp</item>
        <item name="boxStrokeWidthFocused">0dp</item>
    </style>
    
    <style name="ThemeOverlay.MaterialComponents.TextInputEditText.MyStyle" parent="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox.Dense">
        <item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.MyStyle</item>
    </style>
    
    <style name="Widget.MaterialComponents.TextInputEditText.MyStyle" parent="Widget.MaterialComponents.TextInputEditText.FilledBox.Dense">
        <!-- equal padding will center the text -->
        <item name="android:paddingTop">4dp</item>
        <item name="android:paddingBottom">4dp</item>
        <item name="singleLine">true</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2014-03-02
      • 2013-10-15
      • 2020-02-15
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多