无法将样式应用于自定义编辑文本 [英] Cannot apply style for custom edit text

查看:135
本文介绍了无法将样式应用于自定义编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式应用TextInputLayout主题,一次创建一个自定义编辑文本,然后在任何地方使用它.

I'm trying to apply TextInputLayout theme programmatically to create a custom edit text once and use it anywhere.

这是我的自定义"编辑文本类:

This is my Custom edit text class:

package com.enjoyapps.weddingapp.view;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import androidx.appcompat.view.ContextThemeWrapper;

import com.enjoyapps.weddingapp.R;
import com.google.android.material.textfield.TextInputLayout;

public class CustomEditText extends TextInputLayout {


    public CustomEditText(Context context) {
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox));
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
//        super(context, attrs);
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox), attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox), attrs, defStyleAttr);
        init();
    }

    private void init() {
        setBoxStrokeColor(Color.BLUE);
        setBoxCornerRadii(50,50,50,50);
        setBoxBackgroundColor(Color.BLUE);

    }
}

如您所见,在Constructor中,我使用:

As you can see, in Constructor I'm setting the style with :

new ContextThemeWrapper(context, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox)

当我将自定义编辑文本添加到xml时,没有获得我在init方法中设置的属性.

And when i add the custom edit text to xml , it's not getting the attribute that I set in init method.

但是当我在xml中应用相同的主题时,它就可以正常工作,并且可以从init方法中获取all属性.

But when I apply the same theme in xml, it's working and getting the all attribute from init method.

<com.enjoyapps.weddingapp.view.CustomEditText
    android:layout_width="300dp"
    android:layout_centerInParent="true"
    android:layout_height="50dp">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</com.enjoyapps.weddingapp.view.CustomEditText>

推荐答案

花了一些时间才能找出问题所在.显然,TextInputLayout使用boxBackgroundMode以便使用来自不同样式的某些属性.

It took a bit of digging to find out the problem. Apparently, TextInputLayout uses a boxBackgroundMode in order to use certain attributes from different styles.

除了ContextThemeWrapper,还必须为TextInputLayout设置boxBackgroundMode.

In addition to ContextThemeWrapper, you have to set the boxBackgroundMode for your TextInputLayout.

因此,如果您使用的是:

Therefore, if you are using :

  • FilledBox:您需要使用BOX_BACKGROUND_FILLED
  • OutlinedBox:您需要使用BOX_BACKGROUND_OUTLINE
  • FilledBox : you need to use BOX_BACKGROUND_FILLED
  • OutlinedBox : you need to use BOX_BACKGROUND_OUTLINE

在您的情况下,只需将setBoxBackgroundMode(BOX_BACKGROUND_OUTLINE)添加到您的init()中:

In your case, just add setBoxBackgroundMode(BOX_BACKGROUND_OUTLINE) into your init():

private void init() {
    // ... some styling here

    setBoxBackgroundMode(BOX_BACKGROUND_OUTLINE);
}

这篇关于无法将样式应用于自定义编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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