如何在 Android Kotlin 中在没有 MaterialComponents 支持的情况下在 TextInputLayout 上显示边框

问题描述 投票:0回答:1

如何在没有 MaterialComponents 的情况下在 Android 中的 TextInputLayout 上显示边框。

我有一个使用 AppTheme.DialogTheme 的 AppCompatActivity 将 Activity 显示为对话框,但此配置不支持 MaterialComponents。当我使用时:

style =“@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox

应用程序构建成功,但生成运行时错误:

膨胀类 com.google.android.material.textfield.TextInputLayout 时出错

这是使用 MaterialComponent 的 XML 布局:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/inputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginVertical="20dp"
        android:layout_marginTop="10dp"
        app:helperTextEnabled="true"
        app:helperTextTextColor="@color/red">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/newfolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter New Folder"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:maxLength="32"
            android:scrollIndicators="bottom"
            android:scrollbars="horizontal"
            android:scrollHorizontally="true" />
    </com.google.android.material.textfield.TextInputLayout>
kotlin material-components-android android-textinputlayout outline
1个回答
0
投票

我找到了一种无需 MaterialComponents 即可向 TextInputLayout 添加边框的方法。

我向可绘制对象中添加了一个形状。

switch_border.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <stroke
        android:width="2dp"
        android:color="@color/black"/>
</shape>

然后添加到TextInputLayout:

android:background="@drawable/switch_border"

© www.soinside.com 2019 - 2024. All rights reserved.