【问题标题】:How to remove horizontal padding from TextInputLayout?如何从 TextInputLayout 中删除水平填充?
【发布时间】:2020-01-05 01:21:31
【问题描述】:

在迁移到最新版本的 Material Components (1.1.0-alpha09) 后,我注意到 TextInputLayout 有一些填充水平填充。我的目标是实现没有背景或轮廓框的旧式文本字段。所以我扩展了其中一种样式,即Widget.MaterialComponents.TextInputLayout.FilledBox,并将背景颜色设置为透明。

<style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="boxBackgroundColor">@color/transparent</item>
</style>

现在的问题是这个输入字段在我的应用程序中看起来很奇怪,有这样的水平填充(这是填充框或轮廓框所必需的)。

所以我的问题是,我怎样才能删除这个填充?

【问题讨论】:

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


    【解决方案1】:

    你可以使用类似的东西:

    <com.google.android.material.textfield.TextInputLayout
           ....
           android:hint="Hint text"       
           style="@style/My.TextInputLayout.FilledBox.Dense" >
    

    然后您可以使用 materialThemeOverlay 属性定义(需要 1.1.0 版)自定义样式:

      <style name="My.TextInputLayout.FilledBox.Dense" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
        <item name="materialThemeOverlay">@style/MyThemeOverlayFilledDense</item>
      </style>
    
      <style name="MyThemeOverlayFilledDense">
        <item name="editTextStyle">@style/MyTextInputEditText_filledBox_dense
        </item>
      </style>
    
      <style name="MyTextInputEditText_filledBox_dense" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox.Dense">
        <item name="android:paddingStart" ns2:ignore="NewApi">4dp</item>
        <item name="android:paddingEnd" ns2:ignore="NewApi">4dp</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
      </style>
    

    这里是结果(带有默认样式和自定义样式):

    【讨论】:

    • 它有效,谢谢。你能解释一下为什么为 EditText 设置填充不起作用但在主题中设置它是有效的吗?
    • @Nominalista EditText 的一些属性在 TextInputLayout 实现中被覆盖。
    • @GabrieleMariotti 你能解释一下 ns2:ignore="NewApi" 是什么意思吗?我也很好奇,你是怎么发现的?谢谢!
    • @stefan.at.wpf 与@SuppressLint("NewApi") 的含义相同。检查这个link,你可以在android样式中轻松找到它。
    • @GabrieleMariotti 你能告诉我是否可以增加提示文本向左的填充,以使提示文本在 edittext 值的右侧一点点?如果是,我们怎样才能做到这一点?这是我的问题的链接-stackoverflow.com/questions/67502927/…
    猜你喜欢
    • 2018-11-23
    • 2012-12-19
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多