Android 如何修复自定义textinputlayout中的背景红色
Android 如何修复自定义textinputlayout中的背景红色,android,android-edittext,android-textinputlayout,material-components,material-components-android,Android,Android Edittext,Android Textinputlayout,Material Components,Material Components Android,Textinputlayoutbackground在错误发生时为编辑文本提供自定义背景后显示红色 我正在使用自定义textinputlayout放置app:hintEnabled=“false” 可绘制XML文件 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/colorWhite"/> <corners android:r
Textinputlayout
background在错误发生时为编辑文本提供自定义背景后显示红色
我正在使用自定义textinputlayout放置app:hintEnabled=“false”
可绘制XML文件
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorWhite"/>
<corners android:radius="@dimen/margin_4dp"/>
<stroke
android:width="2dp"
android:color="#ff0000"/>
<padding
android:left="8dp"
android:top="10dp"
android:bottom="10dp"
android:right="8dp"/>
如何修复此错误。要更改填充文本字段的背景色,可以:
- 在
文本输入布局上设置
属性boxBackgroundColor
- 使用方法
和textInputLayout.setBoxBackgroundColorResource()
textInputLayout.setBoxBackgroundColor()
TextInputLayout
的笔划,您可以使用:
更改角半径textInputLayout.setBoxCornerRadii()
更改笔划颜色textInputLayout.setBoxStrokeColor()
最后,使用
com.google.android.material.textfield.textinputtext
而不是EditText
来设置文本输入字段的背景颜色,使用正确的文本输入布局格式,而不是下面的代码
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tvFirstNameSecondTest"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/tilLastNameSecondTest"
app:layout_constraintHorizontal_weight="1"
app:hintEnabled="false"
android:layout_marginTop="8dp"
android:layout_marginStart="@dimen/margin_4dp"
android:layout_marginEnd="@dimen/margin_16dp"
app:layout_constraintTop_toBottomOf="@+id/tvFirstNameThirdTest">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_form_filling"
android:id="@+id/etTilLastNameSecondTest"/>
使用以下命令:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tvFirstNameSecondTest"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/tilLastNameSecondTest"
**app:boxBackgroundColor="@color/green"**
app:layout_constraintHorizontal_weight="1"
app:hintEnabled="false"
android:layout_marginTop="8dp"
android:layout_marginStart="@dimen/margin_4dp"
android:layout_marginEnd="@dimen/margin_16dp"
app:layout_constraintTop_toBottomOf="@+id/tvFirstNameThirdTest">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etTilLastNameSecondTest"/>
这些更改是:将EditText替换为“com.google.android.material.textfield.textInputItemText”并添加“app:boxBackgroundColor=“@color/red”。这将消除您所遇到的错误,并且您还可以实现希望InputBox具有的样式您可以在活动主题样式中添加
editTextBackground
颜色,例如
styles.xml文件
<style name="BaseTheme_FullScreen2" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorHint">@color/white</item>
<item name="editTextBackground">@color/yellow</item>
<item name="actionBarSize">48dp</item>
<item name="android:actionBarSize">56dp</item>
<item name="android:windowContentOverlay">@null</item>
<item name="colorControlNormal">@color/bg_color</item>
<item name="colorControlActivated">@color/white</item>
<item name="android:actionMenuTextAppearance">@style/AppTheme.PopupOverlay</item>
</style>
假的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/白色
@颜色/黄色
48dp
56dp
@空的
@颜色/背景颜色
@颜色/白色
@style/AppTheme.PopupOverlay
AndroidManifest.xml
<activity
android:name=".modules.mainModule.homeScreen.HomeActivity"
android:label="@string/title_activity_home"
android:theme="@style/BaseTheme_FullScreen2"
android:windowSoftInputMode="adjustResize|adjustPan"></activity>
对于自定义textinputlayout,您只需要创建一个自定义EditText并重写它的getBackground()方法以返回一个新的drawable。这样,TextInputLayout将无法在EditText的背景上设置颜色过滤器,因为您不返回EditText的背景,而是返回另一个可绘制的。见下文:
@Override
public Drawable getBackground() {
return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}
并在TextInputLayout中使用自定义编辑文本:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_edit_text_bg" />
</android.support.design.widget.TextInputLayout>
感谢兄弟的回复。我想要一个自定义的背景,在那里我需要显示当我得到一个错误。无论如何,通过创建自定义textinputlayout解决了问题。Great@lakshminarayananidadadala如果此解决方案解决了问题,请标记正确或投票。感谢回复兄弟。我也试过了,但后来我创建了一个自定义textinputlayout问题解决了。谢谢你的回复兄弟。我通过创建一个自定义textinputlayout解决了这个问题。
@Override
public Drawable getBackground() {
return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_edit_text_bg" />
</android.support.design.widget.TextInputLayout>