Android 如何以编程方式使用OutlineBox创建TextInputLayout

Android 如何以编程方式使用OutlineBox创建TextInputLayout,android,android-layout,material-design,android-styles,textinputlayout,Android,Android Layout,Material Design,Android Styles,Textinputlayout,我想使用Widget.MaterialComponents.TextInputLayout.OutlinedBox样式创建TextInputLayout。我尝试了很多方法,但都没有达到预期的效果。 这是我的密码 TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox); textInp

我想使用Widget.MaterialComponents.TextInputLayout.OutlinedBox样式创建TextInputLayout。我尝试了很多方法,但都没有达到预期的效果。 这是我的密码

TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
textInputLayout.setHint("My Hint");
TextInputEditText editText = new TextInputEditText(textInputLayout.getContext());
textInputLayout.addView(editText);
parentView.addView(textInputLayout);
我还尝试:

TextInputLayout textInputLayout = new TextInputLayout(getActivity(),null,TextInputLayout.BOX_BACKGROUND_OUTLINE);

我想创建这样的视图。

更新

public class MainActivity extends AppCompatActivity {

    LinearLayout parentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parentView = findViewById(R.id.parentView);

        TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        emailTextInputLayout.setHint("Please Enter Email Address");
        emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
        emailTextInputLayout.addView(edtEmail);

        parentView.addView(emailTextInputLayout);


        TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        passTextInputLayout.setHint("Please Enter Password");
        passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
        passTextInputLayout.addView(edtPass);

        parentView.addView(passTextInputLayout);


    }

}
感谢@Mike M

您需要使用方法来使用OutlineBox样式

setBoxBackgroundMode(int-boxBackgroundMode)

  • 设置框背景的模式(填充、轮廓或无)
然后需要使用常量

注意:要在TextInputLayout的大纲框中找到角点,需要使用

示例代码

public class MainActivity extends AppCompatActivity {

    LinearLayout parentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        parentView = findViewById(R.id.parentView);

        TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        emailTextInputLayout.setHint("Please Enter Email Address");
        emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
        emailTextInputLayout.addView(edtEmail);

        parentView.addView(emailTextInputLayout);


        TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

        passTextInputLayout.setHint("Please Enter Password");
        passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
        passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
        TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
        passTextInputLayout.addView(edtPass);

        parentView.addView(passTextInputLayout);


    }

}
输出

根据这一回答:

  • 当前不支持动态样式更改。必须在创建视图之前设置样式(以XML格式)
这就是
TextInputLayout
不以编程方式接受设置大纲框样式的原因

以下是简单的解决方案:

你可以用

  • 将布局
    XML
    文件实例化为其相应的
    视图
    对象
演示

创建新布局

输出

注 如果要从XML添加
TextInputLayout
,请查看以下答案:

如果您想以编程方式添加5以上的代码> TeXTItPrase,请考虑使用<代码> CurryReVIEW。请查看以下答案:


希望这有帮助

您可以使用
主题
类中定义的方法
applyStyle
。在Kotlin中,您可以通过
上下文
(或子类)实例上的
主题
属性访问它

applyStyle
函数允许您向当前主题添加一个样式,该样式定义了引用样式的主题属性。调用此方法后,您可以将该属性作为
视图的第三个参数传递,如
TextInputLayout
,它将应用所需的样式,同时考虑主题

我在Splitties(我编写的一个库)中使用了这种技术,这里有一些文档和示例可以帮助您:


我还没有在Splitties Views DSL中添加对材质组件主题的一流支持,但您可以自己做,甚至可以打开一个问题来讨论它,或者做出贡献,以便更快地集成它。

我就是这样做的,请注意,必须将TextInputLayout的上下文传递给TextInputItemText,以便正确传递样式

[src:https://material.io/components/text-fields/android#filled-文本字段]

val lp = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
)

val etInputLayout = TextInputLayout(context)
lp.setMargins(16, 16, 16, 16)
etInputLayout.layoutParams = lp
etInputLayout.boxBackgroundMode = TextInputLayout.BOX_BACKGROUND_OUTLINE
etInputLayout.boxBackgroundColor = Color.WHITE
etInputLayout.setBoxCornerRadii(8f, 8f, 8f, 8f)

val etInput = TextInputEditText(etInputLayout.context)
etInput.layoutParams = lp
etInputLayout.addView(etInput, lp)

创建自定义正方形DrawableIt不仅与背景@naveen有关,而且当我们开始使用edittext编写时,这种样式还提示在边框上移动。还可以自动管理焦点更改侦听器等。您可以在LayoutInflater.inflate()的帮助下使用视图作为临时解决方案。您需要使用新的材质设计组件如果您能够找到答案,那将非常棒!谢谢你的帮助。我会尝试你的解决方案,我希望它能奏效。@DeepakVajpayee欢迎告诉我你是否需要任何帮助你的答案对我有用,没有什么变化。由于我在使用androidx,所以我不得不将android.support.design.widget.TextInputLayout更改为com.google.android.material.textfield.TextInputText,将android.support.design.widget.TextInputText更改为**嘿,Nilesh。仅供参考,最新的Material Components
TextInputLayout
具有
setBoxBackgroundMode()
方法,尽管它似乎尚未添加到。我们现在可以通过
setBoxBackgroundMode(TextInputLayout.BOX\u BACKGROUND\u OUTLINE)
以严格的编程方式实现这一点。我刚刚测试过,如果你想更新你的答案,一切都很好。使用
com.google.android.material:material:1.1.0-alpha02
,顺便说一句,不用担心。我只是想在赏金到期前给你弄到赏金。:-)不管怎样,我不确定Kerooker是否真的为他们的设置工作过。哦,好吧。干杯
public class MainActivity extends AppCompatActivity {

    LinearLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootView = findViewById(R.id.rootView);

        View view = LayoutInflater.from(this).inflate(R.layout.temp_layout, null);
        TextInputLayout userNameIDTextInputLayout=view.findViewById(R.id.userIDTextInputLayout);
        TextInputEditText userNameInputEditText = view.findViewById(R.id.userIDTextInputEditText);
        userNameIDTextInputLayout.setHint("Please Enter User Name");
        rootView.addView(view);
    }
}
val lp = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
)

val etInputLayout = TextInputLayout(context)
lp.setMargins(16, 16, 16, 16)
etInputLayout.layoutParams = lp
etInputLayout.boxBackgroundMode = TextInputLayout.BOX_BACKGROUND_OUTLINE
etInputLayout.boxBackgroundColor = Color.WHITE
etInputLayout.setBoxCornerRadii(8f, 8f, 8f, 8f)

val etInput = TextInputEditText(etInputLayout.context)
etInput.layoutParams = lp
etInputLayout.addView(etInput, lp)