【问题标题】:How can I apply a custom shape of TextInputLayout in both the state, Focused and normal state without using librery如何在不使用 librery 的情况下在状态、焦点和正常状态下应用自定义形状的 TextInputLayout
【发布时间】:2020-05-23 22:59:46
【问题描述】:

我想设计一个自定义圆角 TextInputLayout 正如我展示的活动 TextInputLayout 的示例图像below

我在xml中的代码是

<com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/mobile_number"
                android:inputType="number"
                android:fontFamily="@font/montserrat_regular"
                android:maxLength="10" />
</com.google.android.material.textfield.TextInputLayout>

它给了我一个默认的 TextInputLayout。我怎样才能得到我的看法。请帮帮我。 在样式声明中出错。

【问题讨论】:

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


    【解决方案1】:

    只需使用Widget.MaterialComponents.TextInputLayout.OutlinedBox 样式并应用自定义形状:

        <com.google.android.material.textfield.TextInputLayout
            style="@style/OutlinedRoundedBox"
            ...>
    

    与:

     <style name="OutlinedRoundedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="shapeAppearanceOverlay">
          @style/ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded
        </item>
      </style>
    
      <style name="ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">32dp</item>
      </style>
    

    【讨论】:

    • 先生,我在样式声明中遇到错误。正如我在上面的问题中显示的屏幕截图。请再次检查。
    • 非常感谢先生的支持。我的 style.xml 文件是这样的。 &lt;style name="OutlinedRoundBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"&gt; &lt;item name="boxBackgroundMode"&gt;outline&lt;/item&gt; &lt;item name="boxCornerRadiusBottomEnd"&gt;32dp&lt;/item&gt; &lt;item name="boxCornerRadiusBottomStart"&gt;32dp&lt;/item&gt; &lt;item name="boxCornerRadiusTopEnd"&gt;32dp&lt;/item&gt; &lt;item name="boxCornerRadiusTopStart"&gt;32dp&lt;/item&gt; &lt;item name="android:paddingLeft"&gt;20dp&lt;/item&gt; &lt;/style&gt;
    • 要解决您的问题,您必须使用版本 1.1.0
    【解决方案2】:

    如果您想在不使用第三个库的情况下实现设计。只需创建这样的样式:

    <style name="OutlinedRoundBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxBackgroundMode">outline</item>
        <item name="boxCornerRadiusBottomEnd">32dp</item>
        <item name="boxCornerRadiusBottomStart">32dp</item>
        <item name="boxCornerRadiusTopEnd">32dp</item>
        <item name="boxCornerRadiusTopStart">32dp</item>
        <item name="android:paddingLeft">20dp</item>
    </style>
    

    【讨论】:

    • 这在 Android 11 版本 1.3.0 中对我不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多