OutlinedBox TextInputLayout和gravity center是什么?

17

如何在 TextInputLayout 中实现居中的新 OutlinedBox 样式?

当前行为:

左对齐和居中对齐

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout2"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    >
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:hint="hint"
        />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    >
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="hint2"
        />
</android.support.design.widget.TextInputLayout>
我使用 com.android.support:design:28.0.0

有什么想法来实现居中且外观正常的 TextInputEditText 吗?


有没有实现居中且外观正常的TextInputEditText的想法?

1
使用TextInputLayout上的提示,而不是TextInputEditText - Jeel Vankhede
@JeelVankhede,它不起作用,看起来一样。 - Kikju
2个回答

13
使用Material Components Library,从版本1.2.0-alpha02开始,折叠标签的位置是通过编辑文本框的gravity属性确定的。
使用类似以下代码:
<com.google.android.material.textfield.TextInputLayout
    android:hint="@string/...."
    ...>

    <com.google.android.material.textfield.TextInputEditText
        android:gravity="center"
        ../>

</com.google.android.material.textfield.TextInputLayout>
结果是:

enter image description here


当我将 app:hintEnabled="false"app:boxBackgroundMode="none" 设置到 TextInputLayout 时,它无法正常工作。 - Kuvonchbek Yakubov
@KuvonchbekYakubov 很明显。使用 app:hintEnabled="false",布局的浮动标签被禁用,这意味着没有折叠的标签。使用 app:boxBackgroundMode="none",文本输入区域不会被绘制成一个框。这意味着没有轮廓框。 - Gabriele Mariotti

1

在轮廓文本框内部居中的文本并不是我们支持的情况。实际上,在指南中根本没有提到过(https://material.io/design/components/text-fields.html)。

当应用中心重力时,标签文本居中看起来像是一个错误。最简单的解决方法是向我们发送一个包含修复内容的 pull request,位于 https://github.com/material-components/material-components-android。或者您可以在此处报告错误:https://issuetracker.google.com/issues/new?component=439535


这还是现状吗?在后续版本的Material Design中,是否会支持居中的TextInputLayouts? - Alex Conner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接