以编程方式将boxBackgroundMode设置为TextInputLayout [英] Programmatically set boxBackgroundMode to TextInputLayout

查看:63
本文介绍了以编程方式将boxBackgroundMode设置为TextInputLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从 com.android.support:design 迁移到 com.google.android.material .

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'

我无法以编程方式将boxBackgroundMode设置为 TextInputLayout 的概述.

I have the troubles with set up boxBackgroundMode as an outline for TextInputLayout programmatically.

 val textInputLayout = TextInputLayout(this)
 textInputLayout.addView(EditText(this))

 textInputLayout.boxBackgroundMode = TextInputLayout.BOX_BACKGROUND_OUTLINE
 textInputLayout.boxStrokeColor = Color.BLACK
 textInputLayout.boxBackgroundColor = Color.BLACK
 textInputLayout.setBoxCornerRadii(23f,23f,23f,23f)

 someParentLayout.addView(textInputLayout)

与此同时,我没有遇到 com.android.support:design 这样的问题.有人可以建议如何解决或说出 com.google.android.material 无效的原因.

At the same time, I hadn't such problems with com.android.support:design. Can someone suggest how to resolve or tell why for com.google.android.material it doesn't work.

P.S.它通过在xml中定义 style ="@ style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 来工作,但我需要以编程方式完成

P.S. It works by defining style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" in xml but i need to do it programmatically

推荐答案

而不是:

textInputLayout.addView(EditText(this))

执行:

val editText = EditText(this)
editText.background = null
textInputLayout.addView(editText)
// Alternatively `textInputLayout.addView(EditText(this).apply { background = null })`

为什么?

摘录自 TextInputLayout 来源:

  private boolean shouldUseEditTextBackgroundForBoxBackground() {
    // When the text field's EditText's background is null, use the EditText's background for the
    // box background.
    return editText != null
        && boxBackground != null
        && editText.getBackground() == null // <-- by default EditText has background
        && boxBackgroundMode != BOX_BACKGROUND_NONE;
  }

这篇关于以编程方式将boxBackgroundMode设置为TextInputLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆