如何在没有 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>
我找到了一种无需 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"